Ios 当弹出my UIViewController且未加载UIWebView时,应用程序会随机崩溃
我在Ios 当弹出my UIViewController且未加载UIWebView时,应用程序会随机崩溃,ios,objective-c,uiwebview,Ios,Objective C,Uiwebview,我在UITableViewCell中有一个UIWebView,当我尝试在dealloc方法中将web视图的委托设置为nil时,应用程序会随机崩溃。你能告诉我怎么解决这个问题吗 以下是我的dealloc方法的代码: - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; @try { NSLog(@"Dealloc"); for (i
UITableViewCell
中有一个UIWebView
,当我尝试在dealloc
方法中将web视图的委托设置为nil
时,应用程序会随机崩溃。你能告诉我怎么解决这个问题吗
以下是我的dealloc
方法的代码:
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
@try {
NSLog(@"Dealloc");
for (int i=0;i<[arr_searchdocket count];i++) {
cell_webview *cell=[tbl_search cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
[cell.webview stopLoading];
cell.webview.delegate=nil;
NSLog(@"Dealloc row %d",i);
}
[self.view endEditing:YES];
[tbl_search setContentOffset:CGPointZero animated:NO];
tbl_search.dataSource=nil;
tbl_search.delegate=nil;
tbl_search=nil;
self.navigationController.delegate=nil;
}
@catch (NSException *exception) {
}
}
如果您使用的是ARC(如果您不确定,可能是),则不需要将任何委托或数据源设置为
nil
,因为它们是弱引用,并且在解除分配对象时会自动取消设置。我不知道崩溃的确切原因,因为您提供的跟踪中没有消息,但它看起来很像UIWebViewDelegate
中无法识别的选择器。此外--尽管这与您的问题无关--在代码中放入@try{…}@catch(NSException*){}
是一个非常糟糕的主意。在Objective-C中,异常通常是不应捕获的致命错误(通常是程序员错误)。请参阅。@EricGalluzzo[Viewcontroller respondsToSelector:]:消息发送到解除分配的实例0x8a8dcd0这是应用程序crashAha时的日志,确定。Viewcontroller
是实现UIWebViewDelegate
的类的名称吗?如果是这样,则错误可能是因为您在dealloc
方法中调用了[cell.webview stopLoading]
。我将尝试从dealloc
方法中完全删除所有webview逻辑。解除分配UIViewController
时,应自动解除分配UIViewController
中的所有视图和子视图,他们的代理当时应该设置为nil
,因为他们是弱引用。应用程序崩溃发生在一段时间之前,所以我将dealloc逻辑放在另一个viewcontroller中,这样应用程序就不会在该类中崩溃,但会发生在另一个类中,并且我分配了弱引用webview nut not assigned UITableViewCell
Thread : Crashed: com.apple.main-thread
0 libobjc.A.dylib 0x38282f76 objc_msgSend + 21
1 UIKit 0x2d3603ed -[_UIWebViewScrollViewDelegateForwarder forwardInvocation:] + 144
2 CoreFoundation 0x29afe371 ___forwarding___ + 352
3 CoreFoundation 0x29a2d9d8 _CF_forwarding_prep_0 + 24
4 UIKit 0x2d1a575d -[UIScrollView _getDelegateZoomView] + 84
5 UIKit 0x2d1a5699 -[UIScrollView _zoomScaleFromPresentationLayer:] + 24
6 UIKit 0x2d1a5655 -[UIWebDocumentView _zoomedDocumentScale] + 60
7 UIKit 0x2d1a51a5 -[UIWebDocumentView _layoutRectForFixedPositionObjects] + 104
8 UIKit 0x2d1a5007 -[UIWebDocumentView _updateFixedPositionedObjectsLayoutRectUsingWebThread:synchronize:] + 38
9 UIKit 0x2d1a7445 -[UIWebDocumentView _updateFixedPositioningObjectsLayoutAfterScroll] + 28
10 UIKit 0x2d1a7421 -[UIWebBrowserView _updateFixedPositioningObjectsLayoutAfterScroll] + 56
11 CoreFoundation 0x29ab30c5 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
12 CoreFoundation 0x29a0ccbd _CFXNotificationPost + 1800
13 Foundation 0x2a76f3a1 -[NSNotificationCenter postNotificationName:object:userInfo:] + 76
14 UIKit 0x2d7c16c7 -[UIInputWindowController postEndNotifications:withInfo:] + 554
15 UIKit 0x2d7bff15 -[UIInputWindowController performWithSafeTransitionFrames:] + 136
16 UIKit 0x2d7c3019 __77-[UIInputWindowController moveFromPlacement:toPlacement:starting:completion:]_block_invoke644 + 428
17 UIKit 0x2d18f8e1 -[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:] + 308
18 UIKit 0x2d18f4f9 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 184
19 UIKit 0x2d18f40f -[UIViewAnimationState animationDidStop:finished:] + 70
20 QuartzCore 0x2cb74fe7 CA::Layer::run_animation_callbacks(void*) + 234
21 libdispatch.dylib 0x38806c6f _dispatch_client_callout + 22
22 libdispatch.dylib 0x38812553 _dispatch_main_queue_callback_4CF$VARIANT$mp + 1322
23 CoreFoundation 0x29ac0889 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
24 CoreFoundation 0x29abefa9 __CFRunLoopRun + 1512
25 CoreFoundation 0x29a0a9a1 CFRunLoopRunSpecific + 476
26 CoreFoundation 0x29a0a7b3 CFRunLoopRunInMode + 106
27 GraphicsServices 0x313a31a9 GSEventRunModal + 136
28 UIKit 0x2d1bb635 UIApplicationMain + 1440
29 TaskTower 0xc4379 main (main.m:14)
30 libdyld.dylib 0x38844aaf start + 2