How change this from UIWebView to WKWebView? - objective-c

I have tried with the 3 or 4 posts about this here but none use file path and I cannot get this working with WKWebView, it works with UIWebView, please can someone help me.
Yes, I am very new to this, I tried all day before posting here tonight, so easy to understand instructions would be great. Thanks.
.h file:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIWebView *contentWebView;
#end
.m file:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize contentWebView;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *filePath = [[NSBundle mainBundle]pathForResource:#"LockBackground" ofType:#"html"];
NSURL * fileURL = [NSURL fileURLWithPath:filePath isDirectory:NO];
NSURLRequest * myNSURLRequest = [[NSURLRequest alloc]initWithURL:fileURL];
[contentWebView loadRequest:myNSURLRequest];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Simply changing
#property (weak, nonatomic) IBOutlet UIWebView *contentWebView;
to
#property (weak, nonatomic) IBOutlet WKWebView *contentWebView;
should work.
If you intend to provide support for both iOS 7 and iOS 8, you'll have to declare two variables and add the following verification:
if ([WKWebView class]) {
// do new webview stuff
}
else {
// do old webview stuff
}

Related

EXEC_BAD_ACCESS when clicking on a button in objective C

I am just learning and trying to put forth some of the knowledge with objective c and cocoa. I have a little program that has a login screen which then displays a main menu. On the main menu view, there is a button that should load a view to be able to add some data to the app. When I click on the button, it doesn't fire the IBAction but throws an error
EXC_BAD_ACCESS [StartMenuViewController performSelector:withObject:]: message sent to deallocated instance 0x6080195e9f90
I know that it is because there is an object that is not instantiated when the message is being sent, but I cannot find out why. Sorry for the code and the novice level here is the main menu view controller StartMenuViewController.m file:
#import "StartMenuViewController.h"
#import "AppDelegate.h"
#import "MasterViewController.h"
#interface StartMenuViewController ()
#end
#implementation StartMenuViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
}
-(IBAction)showStrainView:(id)sender{
AppDelegate *delegate = nil;
delegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
MasterViewController *addStrainView = [[MasterViewController alloc] initWithNibName:#"MasterViewController" bundle:nil];
[delegate swapView:self.view toView:addStrainView.view];
}
#end
here is the StartMenuViewController.h
#import <Cocoa/Cocoa.h>
#interface StartMenuViewController : NSViewController
#property (weak) IBOutlet NSButton *showStrainViewButton;
#end
Here is the AppDelegate.h
#import <Cocoa/Cocoa.h>
#class loginView, MasterViewController,StartMenuViewController;
#interface AppDelegate : NSObject <NSApplicationDelegate>{
loginView *loginView;
MasterViewController *masterViewController;
}
#property (assign) IBOutlet NSWindow *window;
#property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
#property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
#property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
#property (strong) loginView *loginView;
#property (strong) MasterViewController *masterViewController;
#property (strong) StartMenuViewController *starMenuViewController;
-(void)swapView:(NSView *)view1 toView:(NSView *)view2;
#end
Here is the AppDelegate.m
#import "AppDelegate.h"
#include "MasterViewController.h"
#import "ScaryBugDoc.h"
#import "Strains.h"
#import "loginView.h"
#interface AppDelegate()
#end
#implementation AppDelegate
#synthesize managedObjectContext = _managedObjectContext;
#synthesize managedObjectModel = _managedObjectModel;
#synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
#synthesize loginView;
#synthesize masterViewController;
#synthesize starMenuViewController;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// 1. Create the master View Controller
self.loginView = [[loginView alloc] initWithNibName:#"loginView" bundle:nil];
// 2. Add the view controller to the Window's content view
[self.window.contentView addSubview:self.loginView.view];
self.loginView.view.frame = ((NSView*)self.window.contentView).bounds;
}
-(void)swapView:(NSView *)view1 toView:(NSView *)view2{
//[view1 removeFromSuperview];
[self.window.contentView addSubview:view2];
view2.frame = ((NSView*)self.window.contentView).bounds;
}
#end
If you need the loginView.h and loginView.m files I can add them as well. I would appreciate any and all help. Thanks in advance
The problem is here:
MasterViewController *addStrainView = [[MasterViewController alloc]initWithNibName:#"MasterViewController" bundle:nil];
[delegate swapView:self.view toView:addStrainView.view];
You don't keep a reference to the MasterViewController so it gets deallocated even though its view is kept around. Now anytime the view tries to access methods from the view controller, things fail.
You should make addStrainView a child view controller of self in the above code so the view controller sticks around as long as its view.

push segue causing break point

hoepfully a simple fix. :)
I'm using a collection view with a push segue to a detailed view controller. When the cell is clicked in simulator the app crashes with a break point indicating at this part of the code:
#import "DetailViewController.h"
#interface DetailViewController ()
#property (nonatomic, weak) IBOutlet UIImageView *imageView;
#end
#implementation DetailViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated]; //<--------- break point
self.imageView.image = self.image;
}
#end
segue id etc is correct. So not at all sure why it shouldn't be working.
Any help on this as always is appreciated. If you need further info let me know.
A.
edit:
.h file as follows:
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController
#property (nonatomic, strong) UIImage *image;
#end

Multiple windows mac application

I want to make a multiple windows mac application and I am stuck with the multiple window part !
I can display some windows by creating a xib file and using this method :
-(void)popView:(NSString *)viewName {
_windowController = [[AddProdutWindowController alloc] initWithWindowNibName:viewName];
[_windowController showWindow:nil];
}
With
#property (strong, nonatomic) AddProdutWindowController *windowController;
in my header file and AddProductViewController inherit from NSWindowViewController
I have linked a subclass of NSViewController in Xcode to my xib file.
Now I want to send some datas to my new view and show them in some NSTextField and I have not a single clue how to do it !
I'm very confused with the WindowsController and ViewController, I don't exactly know how / where use them.
Thank you for helping.
Try this:
your firstWindowController.h:
#import <Cocoa/Cocoa.h>
#class SecondWindowController;
#interface ResultWindowController : NSWindowController{
SecondWindowController *swc;
}
#property (assign) NSNumber *xyz;
...
your firstWindowController.m:
#import "FirstWindowController.h"
#import "SecondWindowController.h"
#implementation FirstWindowController
#synthesize xyz;
- (IBAction)openSecondWindow:(id *)sender {
if (!swc)
{
swc = [[SecondWindowController alloc] init];
}
[Swc setFwc:self];
[Swc showWindow:self];
your secondWindowController.h:
#import <Cocoa/Cocoa.h>
#class FirstWindowController;
#interface SecondWindowController : NSWindowController {
FirstWindowController *fwc;
}
#property (retain) IBOutlet FirstWindowController *fwc;
#end
your secondWindowController.m:
#import "SecondWindowController.h"
#import "FirstWindowController.h"
#implementation SecondWindowController
#synthesize fwc;
- (id)init
{
if(![super initWithWindowNibName:#"SecondWindow"])
return nil;
NSLog(#"_init: %#", NSStringFromClass([self class]));
return self;
}
- (void)windowDidLoad
{
[super windowDidLoad];
NSLog(#"swc didload self=%p", self); //thats your second window controller
NSLog(#"fwc value is %#", fwd.xyz); // here you should be able to see the value from FirtsWindowController
}

UITextField: set text from a label

I'm trying to write my first iPad app using Xcode 4: it's based on "Tabbed Application" template with two views. On the first view, user selects a City (label displays selection) and on the second wiew there is a IBOutletCollection(UITextField) NSArray *playersData. I want to set the city selected on first view as a default city on second view. I have checked storyboard connections and they seem to be ok.
I get nothing. Any idea?
First view :
#import
#interface pruebaF1FirstViewController : UIViewController
- (IBAction)selectCity:(UIButton *)sender;
#property (weak, nonatomic) IBOutlet UILabel *citySelected;
#end
First view implementation :
#import "pruebaF1FirstViewController.h"
#import "pruebaF1SecondViewController.h"
#interface pruebaF1FirstViewController ()
#property pruebaF1SecondViewController *secondView;
#end
#implementation pruebaF1FirstViewController
#synthesize citySelected;
#synthesize secondView;
- (void)viewDidLoad
...
- (IBAction)selectCity:(UIButton *)sender {
NSString *currentCity =[sender currentTitle];
citySelected.text=#"";
citySelected.text =[citySelected.text stringByAppendingString:currentCity];
/*writes null*/
secondView.defaultCity.text=[NSString stringWithFormat:#"%#" ,currentCity];
NSLog(#"%#",secondView.defaultCity.text);
}
#end
Second view header
#import <UIKit/UIKit.h>
#interface pruebaF1SecondViewController : UIViewController <UITextFieldDelegate>
#property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *playersData;
#property (retain, nonatomic) IBOutlet UITextField *defaultCity;
- (IBAction)eraseData:(UIButton *)sender;
- (IBAction)savePlayersData:(UIButton *)sender;
- (IBAction)termsPopUp:(UIButton *)sender;
/*- (void)writeDefaultCity:(NSString *)currentCity;*/
#end
Second view implementation
#import "pruebaF1SecondViewController.h"
#import "pruebaF1FirstViewController.h"
#interface pruebaF1SecondViewController ()
#property (nonatomic, strong)pruebaF1FirstViewController *prueba;
#end
#implementation pruebaF1SecondViewController
#synthesize playersData;
#synthesize defaultCity;
#synthesize prueba;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[self setPlayersData:nil];
[self setDefaultCity:nil];
[super viewDidUnload];
}
/* Return or Done button dismiss keyboard*/
-(BOOL)textFieldShouldReturn:(UITextField *)boxes
{
for (UITextField *boxes in playersData) {
[boxes resignFirstResponder];
}
return YES;
}
....
/*Trying to get city's name from first view in two ways*/
/*-(void)setDefaultCity
{
NSString *defecto=prueba.citySelected.text;
self.defaultCity.text = defecto;
}*/
- (IBAction)eraseData:(UIButton *)sender {
for (UITextField *boxes in playersData) {
boxes.text = #" ";
}
}
/*
-(void)writeDefaultCity:(NSString *)currentCity
{
defaultCity.text =[NSString stringWithFormat:#"%#" ,currentCity];
NSLog(#"Ciudad elegida: %#",currentCity);
}*/
....
#end
Views are generally not loaded until they are displayed on the device, and may be unloaded at any time when not visible to save memory. This means that when you're setting the 'text' property of the 'defaultCity', that label has not yet been created.
Instead you should add a NSString * property to pruebaF1SecondViewController and set the value of the defaultCity label in viewDidLoad:
In the header:
#property (nonatomic, weak) UILabel *defaultCityLabel;
#property (nonatomic, strong) NSString *defaultCity;
In the implementation
- (void)viewDidLoad
{
[super viewDidLoad];
defaultCityLabel.text = defaultCity;
}
And in the first view controller, assign the string value instead of the label value.

Trying to import a custom UITableViewCell keeps giving me a file not found lexical preprocessor error in Objective-C

Trying to import a custom UITableViewCell and it keeps giving me a file not found lexical preprocessor error. I can't figure it out.
ProductCell.h
#import <UIKit/UIKit.h>
#interface ProductCell : UITableViewCell {
IBOutlet UILabel *descript;
IBOutlet UILabel *productCode;
IBOutlet UIImageView *prodImage;
}
#property (nonatomic, retain) IBOutlet UILabel *descript;
#property (nonatomic, retain) IBOutlet UILabel *productCode;
#property (nonatomic, retain) IBOutlet UIImageView *prodImage;
#end
ProductCell.m
#import "ProductCell.h"
#implementation ProductCell
#synthesize descript;
#synthesize productCode;
#synthesize prodImage;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc
{
[super dealloc];
}
#end
In my UItableviewcontroller.h file I've tried to import as #class and not doesnt seem to make a difference. and in my implementation file i simply
#import "ProductCell.h"
why is this? what basic step am I missing. Importing it in implementation file should solve my issue. Tried cleaning project
Renaming the header file to something else and renaming it back to its original name did solve the problem for me.
This appears to be an XCode4 bug. Despite the fact that the compiler shows an error, if you try running the project, it should work.
This is discussed in the Apple forums in the following places:
https://devforums.apple.com/message/369278#369278
https://devforums.apple.com/message/379512#379512
https://devforums.apple.com/message/382662#382662