I created an ARC application that works great. It has a UINavigationController which I use to push views, and everything works fine.
I am converting an application to iPad and I decided to show one of the views as a popover. (I don't like the UIPopoverController, so I created my own custom popup). He added to the view as follows.
MyViewController *hotelinf = [[MyViewController alloc] initWithNibName:@"MyViewController_iPad" bundle:nil]; [self.view addSubview:hotelinf.view];
The view is added as a wonderful substrate. The view I'm adding contains a UIWebView that has the usual delegate methods, but when the view tries to access one of the delegates, it just crashes.
*** -[MyViewController respondsToSelector:]: message sent to deallocated instance 0x7917b90
In particular, it crashes on this line.
[self.webView loadHTMLString:stringResponse baseURL:nil]
I looked at the views (and UINavigationControllers) as subViews many times without any problems, although none of them included a UIWebView delegate. I assume that I need to set the delegate of my UIViewController istance, but I'm not sure how to do this. It is also worth noting that if I click on a view in an existing UINavigationController, it will call and load the HTML file, which certainly means that it has nothing to do with the code of the view itself.
Any help would be greatly appreciated! Here is the code (in addition to the above that the controller shows).
.h
@interface MyViewController : UIViewController <UIWebViewDelegate, UIActionSheetDelegate> { //Unrelated IBOutlets } @property (nonatomic, strong) UIWebView *webView; @end
.m
@implementation MyViewController @synthesize webView; - (void)viewDidLoad { [super viewDidLoad]; self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(317,283,393,354)]; self.webView.delegate = self; [self.view addSubview:self.webView]; [NSThread detachNewThreadSelector:@selector(getHTMLString) toTarget:self withObject:nil]; } -(void)getHTMLString { @autoreleasepool { //Download a valid HTML String [self performSelectorOnMainThread:@selector(loadHTML) withObject:nil waitUntilDone:NO]; } } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; self.webView = nil; } -(void)loadHTML { self.webView.opaque = NO; self.webView.backgroundColor = [UIColor clearColor]; if ([stringResponse isEqualToString:@""]) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Could not connect to XXXXX.com. Please verify you are connected to a working 3G/WIFI Network." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; } else { //it crashes here only when loaded as a subview - the first access to the delegate [self.webView loadHTMLString:stringResponse baseURL:nil]; } } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)webViewDidFinishLoad:(UIWebView *)webView { [self stopIndicator]; } - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { if (error.code == NSURLErrorCancelled) return; // this is Error -999 [self stopIndicator]; // report the error inside the webview NSString* errorString = [NSString stringWithFormat: @"<html><center><font size=+10 color='black' face='Helvetica'>An error occurred:<br>%@</font></center></html>", error.localizedDescription]; [self.webView loadHTMLString:errorString baseURL:nil]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot load URL." message:@"You have a connection failure. Please verify you are connected to a WIFI or 3G enabled Network." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; } @end