我正在将应用程序转换为iPad,我决定将其中一个视图显示为popover. (我不喜欢UIPopoverController所以我创建了自己的基本d出窗口).它被添加到视图中如下..
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)显示为subVIEws而没有任何问题,尽管它们都没有包含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解决方法 该问题与UIWebVIEw无关,而与您的控制器类无关.确实,
MyVIEwController *hotelinf = [[MyVIEwController alloc] initWithNibname:@"MyVIEwController_iPad" bundle:nil];[self.vIEw addSubvIEw:hotelinf.vIEw];
您正在分配控制器并将其分配给局部变量;然后将控制器的视图作为子视图添加到当前视图中.这样做,该视图被保留,但控制器对象本身会发生什么?你发布了吗?或者它泄漏(因为它被分配给局部变量)?
这可能解释了为什么稍后调用respondsToSelector方法时,控制器已被解除分配…
解决这个问题的方法是在主控制器类中创建一个新属性或ivar,并在其中存储MyVIEwController.不要忘记以dealloc发布它.
我还会建议另一件事.在:
- (voID)vIEwDIDdisappear:(BOol)animated{ [super vIEwDIDdisappear:animated]; self.webVIEw = nil;}
在释放视图之前将webVIEw委托设置为nil:
- (voID)vIEwDIDdisappear:(BOol)animated{ [super vIEwDIDdisappear:animated]; self.webVIEw.delegate = nil; self.webVIEw = nil;}
我还可能会查看您在vIEwDIDdisappear中发布webVIEw的原因.另一方面,您在vIEwDIDLoad中分配它.这种不对称是危险的,因为每当主视图消失(出于任何原因)时,webVIEw将被删除,当视图重新出现时,它就不再存在了.
总结以上是内存溢出为你收集整理的iphone – 令人沮丧的UIWebView委托崩溃问题全部内容,希望文章能够帮你解决iphone – 令人沮丧的UIWebView委托崩溃问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)