【发布时间】:2012-01-24 22:22:57
【问题描述】:
我创建了一个运行完美的 ARC 应用程序。它有一个 UINavigationController,我用它来推送视图,一切运行良好。
我正在将应用程序转换为 iPad,并且我决定将其中一个视图显示为弹出框。 (我不喜欢 UIPopoverController 所以我创建了自己的基本弹出窗口)。它被添加到视图中,如下所示..
MyViewController *hotelinf = [[MyViewController alloc]
initWithNibName:@"MyViewController_iPad" bundle:nil];
[self.view addSubview:hotelinf.view];
视图被添加为子视图。我添加的视图包含一个 UIWebView,其中包含通常的委托方法,但是当视图尝试访问其中一个委托时,它只会崩溃。
*** -[MyViewController respondsToSelector:]: message sent to deallocated instance 0x7917b90
具体来说,它在这一行崩溃..
[self.webView loadHTMLString:stringResponse baseURL:nil];
我已经多次将视图(和 UINavigationControllers)显示为子视图,没有任何问题,尽管它们都没有包含 UIWebView 委托。我猜我必须设置我的 UIViewController istance 的委托,但我不确定如何。还值得注意的是,如果我在现有的 UINavigationController 中推送视图,它会调用并加载 HTML,这肯定意味着它与视图本身的代码无关。
任何帮助将不胜感激!这是代码(除了上面显示的控制器)..
.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
【问题讨论】:
标签: iphone objective-c ios xcode delegates