Reputation: 537
I'm trying to use the UIWebViewDelegate, however, when I set delegate to it's self, I get a 'message sent to deallocated instance' error.
Actual error: "2014-04-07 22:12:05.402 AppName[746:60b] -[WebViewController respondsToSelector:]: message sent to deallocated instance 0xa47ae00"
I presume this is because the WebViewController instance has not been retained?
Could anyone point me in the right direction for this please?
My structure is: RootViewController > WebViewController (delegate) > WebView
// root.h
@interface RootViewController : UIViewController
@property (strong, nonatomic) WebViewController * webViewController;
@end
// root.m
#import "RootViewController.h"
#import <UIKit/UIKit.h>
#import "WebViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad
{
_webViewController = [[WebViewController alloc] init];
[self.view addSubview:_webViewController.view];
}
@end
// .h
#import <UIKit/UIKit.h>
@interface WebViewController : UIViewController <UIWebViewDelegate>
@property(strong) UIWebView *webView;
@end
// .m
#import "WebViewController.h"
@interface WebViewController ()
@end
@implementation WebViewController
- (void)viewDidLoad
{
_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
_webView.delegate = self;
NSString *url = @"http://google.com/";
NSURL *nsurl = [NSURL URLWithString:url];
NSURLRequest *nsrequest = [NSURLRequest requestWithURL:nsurl];
[_webView loadRequest:nsrequest];
[self.view addSubview:_webView];
}
-(void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(@"ui web view started load");
}
Upvotes: 0
Views: 1870
Reputation: 537
I had not set the root view controller like so:
self.window.rootViewController = rootController;
Upvotes: 0
Reputation: 3521
Try moving your code out of loadView
into viewDidLoad
, and remove your calls to loadView
. I believe loadView
is called automatically when the view controller is instantiated and shown, so calling it again might cause you to instantiate your webview twice.
From the docs:
You should never call this method directly. The view controller calls this method when its view property is requested but is currently nil. This method loads or creates a view and assigns it to the view property.
Upvotes: 1