今天在写UIPopoverDemo时,触发弹出popover时,程序crash。
堆栈信息如下:
Terminating app due to uncaught exception ‘NSGenericException‘, reason: ‘-[UIPopoverController dealloc] reached while popover is still visible.‘
*** First throw call stack:
(
0 CoreFoundation 0x0173b5e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014be8b6 objc_exception_throw + 44
2 CoreFoundation 0x0173b3bb +[NSException raise:format:] + 139
3 UIKit 0x007fab16 -[UIPopoverController dealloc] + 86
4 libobjc.A.dylib 0x014cf692 _ZN11objc_object17sidetable_releaseEb + 268
5 libobjc.A.dylib 0x014cee81 objc_release + 49
6 libobjc.A.dylib 0x014cee3e objc_storeStrong + 39
7 TXUIPopoverDemo 0x00003694 -[TXContailnerViewController popoBtnClicked] + 468
8 libobjc.A.dylib 0x014d0874 -[NSObject performSelector:withObject:withObject:] + 77
9 UIKit 0x0022e0c2 -[UIApplication sendAction:to:from:forEvent:] + 108
10 UIKit 0x0022e04e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
11 UIKit 0x003260c1 -[UIControl sendAction:to:forEvent:] + 66
12 UIKit 0x00326484 -[UIControl _sendActionsForEvents:withEvent:] + 577
13 UIKit 0x00325733 -[UIControl touchesEnded:withEvent:] + 641
14 UIKit 0x0026b51d -[UIWindow _sendTouchesForEvent:] + 852
15 UIKit 0x0026c184 -[UIWindow sendEvent:] + 1232
16 UIKit 0x0023fe86 -[UIApplication sendEvent:] + 242
17 UIKit 0x0022a18f _UIApplicationHandleEventQueue + 11421
18 CoreFoundation 0x016c483f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
19 CoreFoundation 0x016c41cb __CFRunLoopDoSources0 + 235
20 CoreFoundation 0x016e129e __CFRunLoopRun + 910
21 CoreFoundation 0x016e0ac3 CFRunLoopRunSpecific + 467
22 CoreFoundation 0x016e08db CFRunLoopRunInMode + 123
23 GraphicsServices 0x036909e2 GSEventRunModal + 192
24 GraphicsServices 0x03690809 GSEventRun + 104
25 UIKit 0x0022cd3b UIApplicationMain + 1225
26 TXUIPopoverDemo 0x00003c9d main + 141
27 libdyld.dylib 0x01d77725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException
从字面意思来看,是UIPopoverController可见的时候被释放掉了,可能是释放不当的原因。google了下,发现答案很多,哈哈
分析了代码片段
popoverVC是一个局部变量,等popoBtnClicked方法执行完后,VC就自动释放了,但popoverVC的view还被window retain着,所以crash了。
所以要修改是让popoverVC不要被释放,所以把它作为一个property被当前vc持有。
修改后如下:
TXContentViewController * contentVC = [[TXContentViewControlleralloc]initWithNibName:nilbundle:nil];
self.popoverVCA = [[UIPopoverControlleralloc]initWithContentViewController:contentVC];
self.popoverVCA.backgroundColor = [UIColorpurpleColor];
[self.popoverVCApresentPopoverFromRect:CGRectMake(200,200, 100,100) inView:self.viewpermittedArrowDirections:UIPopoverArrowDirectionAnyanimated:YES] ;
参考资料:http://*.com/questions/8609122/error-using-uipopovercontroller