EXC_BAD_ACCESS 带有手势识别器和故事板
我有一个以编程方式创建的故事板(因为我只将它用于 iOS 5),其中包含一个简单的控制器和一个 iPad 视图。我需要一个手势识别器并添加了它。但是当应用程序加载并且我使味道更大时,我得到 EXC_BAD_ACCESS:
*** -[DOiPadStoryboardViewController handleSliding:]:发送到已释放实例 0x238580 的消息
这一定是一些简单的事情,我做错了。为什么控制器被释放?
我使用以下代码创建故事板:
if (isIOS5) {
// iOS 5 with storyboard
UIViewController *viewControler = nil;
if([self isiPad]){
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPadStoryboard" bundle: [NSBundle mainBundle]];
viewControler = [storyboard instantiateInitialViewController];
} else {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPhoneStoryboard" bundle: [NSBundle mainBundle]];
viewControler = [storyboard instantiateInitialViewController];
}
[self.window addSubview:viewControler.view];
}
手势识别器连接到主视图,IBAction 位于视图控制器中。故事板如下所示(但不确定这是否会添加任何内容):
这是平移手势识别器:
最后是“bt”控制台输出:
#0 0x37d2b8a0 in ___forwarding___ ()
#1 0x37c86680 in __forwarding_prep_0___ ()
#2 0x318d2f06 in _UIGestureRecognizerSendActions ()
#3 0x31864c1c in -[UIGestureRecognizer _updateGestureWithEvent:] ()
#4 0x31a90508 in ___UIGestureRecognizerUpdate_block_invoke_0541 ()
#5 0x317dfd68 in _UIGestureRecognizerApplyBlocksToArray ()
#6 0x317de8b6 in _UIGestureRecognizerUpdate ()
#7 0x317eb3cc in _UIGestureRecognizerUpdateGesturesFromSendEvent ()
#8 0x317eb20e in -[UIWindow _sendGesturesForEvent:] ()
#9 0x317eaddc in -[UIWindow sendEvent:] ()
#10 0x317d14ec in -[UIApplication sendEvent:] ()
#11 0x317d0d2c in _UIApplicationHandleEvent ()
#12 0x37a57df2 in PurpleEventCallback ()
#13 0x37cfd552 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#14 0x37cfd4f4 in __CFRunLoopDoSource1 ()
#15 0x37cfc342 in __CFRunLoopRun ()
#16 0x37c7f4dc in CFRunLoopRunSpecific ()
#17 0x37c7f3a4 in CFRunLoopRunInMode ()
#18 0x37a56fcc in GSEventRunModal ()
#19 0x317ff742 in UIApplicationMain ()
#20 0x00003050 in main (argc=1, argv=0x2fdffaa8)
I have a programmatically created Storyboard (as I only use it for iOS 5) with a simple Controller and a iPad View. I need a gesture recognizer and added that. But when the App loads and I make the gustier I get EXC_BAD_ACCESS:
*** -[DOiPadStoryboardViewController handleSliding:]: message sent to deallocated instance 0x238580
It must be something simple I am doing wrong. Why is the controller deallocated?
I create the storyboard with the following code:
if (isIOS5) {
// iOS 5 with storyboard
UIViewController *viewControler = nil;
if([self isiPad]){
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPadStoryboard" bundle: [NSBundle mainBundle]];
viewControler = [storyboard instantiateInitialViewController];
} else {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"iPhoneStoryboard" bundle: [NSBundle mainBundle]];
viewControler = [storyboard instantiateInitialViewController];
}
[self.window addSubview:viewControler.view];
}
The gesture recognizer is connected to the main view and the IBAction is in the view controller. The storyboard looks as follows (but not sure if this add anything):
and this is the Pan Gesture Recognizer:
and finally the 'bt' console output:
#0 0x37d2b8a0 in ___forwarding___ ()
#1 0x37c86680 in __forwarding_prep_0___ ()
#2 0x318d2f06 in _UIGestureRecognizerSendActions ()
#3 0x31864c1c in -[UIGestureRecognizer _updateGestureWithEvent:] ()
#4 0x31a90508 in ___UIGestureRecognizerUpdate_block_invoke_0541 ()
#5 0x317dfd68 in _UIGestureRecognizerApplyBlocksToArray ()
#6 0x317de8b6 in _UIGestureRecognizerUpdate ()
#7 0x317eb3cc in _UIGestureRecognizerUpdateGesturesFromSendEvent ()
#8 0x317eb20e in -[UIWindow _sendGesturesForEvent:] ()
#9 0x317eaddc in -[UIWindow sendEvent:] ()
#10 0x317d14ec in -[UIApplication sendEvent:] ()
#11 0x317d0d2c in _UIApplicationHandleEvent ()
#12 0x37a57df2 in PurpleEventCallback ()
#13 0x37cfd552 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#14 0x37cfd4f4 in __CFRunLoopDoSource1 ()
#15 0x37cfc342 in __CFRunLoopRun ()
#16 0x37c7f4dc in CFRunLoopRunSpecific ()
#17 0x37c7f3a4 in CFRunLoopRunInMode ()
#18 0x37a56fcc in GSEventRunModal ()
#19 0x317ff742 in UIApplicationMain ()
#20 0x00003050 in main (argc=1, argv=0x2fdffaa8)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将视图控制器设置为窗口的根视图控制器,而不是将其视图添加为子视图 - Apple 已过时且不鼓励这样做,因为您要将视图层次结构与视图控制器层次结构分离,这最终会导致令人讨厌的错误,例如因为您的视图控制器没有获得旋转回调等。
顺便说一句,您的视图控制器将被窗口保留,并且不再崩溃。
Set the view controller's to be your window's root view controller, instead of adding its view as a subview - which is obsolete and discouraged by Apple since you are detaching the views hierarchy from the view controllers hierarchy, which in turn will eventually cause nasty bugs such as your view controllers not getting rotation callbacks, etc. etc.
As a by the way, your view controller will be retained by the window, and no more crash.
只需将 viewControler 设为(保留的)属性即可。您没有在代码示例中保留视图控制器。
在 .h 文件中创建属性:
在 .m 文件中添加:
同时更改分配:
Just make viewControler a (retained) property. You are not retaining the view controller in your code example.
Create a property in the .h file:
In the .m file add:
Also change the assignment: