View Controller即使已被解除分配也会收到消息 [英] View Controller being sent a message even though it has been deallocated

查看:90
本文介绍了View Controller即使已被解除分配也会收到消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定iPhone SDK 3.0中是否有任何变化,但我收到了最奇怪的错误。我有一个视图控制器层次结构,我根据接口方向在视图控制器之间切换。据我所知,每当我旋转接口时,就会发生错误,已经解除分配的视图控制器正在发送一个shouldAutorotateToInterfaceOrientation消息。这是错误的回溯:

I am not sure if something has changed in the iPhone SDK 3.0 but I am getting the strangest error. I have a view controller hierarchy where I switch between view controllers depending upon interface orientation. From what I can tell, the error is caused whenever I rotate the interface a view controller which has been deallocated is being sent a shouldAutorotateToInterfaceOrientation message. This is the backtrace for the error:

#0 0x01dc43a7 in ___forwarding___
#1 0x01da06c2 in __forwarding_prep_0___
#2 0x002e6733 in -[UIWindow _shouldAutorotateToInterfaceOrientation:]
#3 0x002e6562 in -[UIWindow _updateToInterfaceOrientation:duration:force:]
#4 0x002e6515 in -[UIWindow _updateInterfaceOrientationFromDeviceOrientation]
#5 0x0004d63a in _nsnote_callback
#6 0x01d8f005 in _CFXNotificationPostNotification
#7 0x0004aef0 in -[NSNotificationCenter postNotificationName:object:userInfo:]
#8 0x0045b454 in -[UIDevice setOrientation:]
#9 0x002d6890 in -[UIApplication handleEvent:withNewEvent:]
#10 0x002d16d3 in -[UIApplication sendEvent:]
#11 0x002d80b5 in _UIApplicationHandleEvent
#12 0x024c2ef1 in PurpleEventCallback
#13 0x01d9bb80 in CFRunLoopRunSpecific
#14 0x01d9ac48 in CFRunLoopRunInMode
#15 0x024c17ad in GSEventRunModal
#16 0x024c1872 in GSEventRun
#17 0x002d9003 in UIApplicationMain
#18 0x00002d50 in main at main.m:14

正在获取的错误使用NSZombieEnabled打印到调试控制台是:

The error that is getting printed to the Debug Console with NSZombieEnabled is:

2009-10-18 20:28:34.404 Restaurants[12428:207] *** -[ToolbarController respondsToSelector:]: message sent to deallocated instance 0x3b2b2a0
(gdb) continue
Current language:  auto; currently objective-c
2009-10-18 20:31:43.496 Restaurants[12428:207] *** NSInvocation: warning: object 0x3b2b2a0 of class '_NSZombie_BeltToolbarController' does not implement methodSignatureForSelector: -- trouble ahead
2009-10-18 20:31:43.496 Restaurants[12428:207] *** NSInvocation: warning: object 0x3b2b2a0 of class '_NSZombie_BeltToolbarController' does not implement doesNotRecognizeSelector: -- abort

我无法理解的是,为什么系统试图向该控制器发送消息,即使它已被解除分配并且有办法告诉系统控制器不再存在。

What I can't understand is why the system is trying to message this controller even though it has been deallocated and is there a way to tell the system that the controller doesn't exist any longer.

[更新]:
我已经整理了一个复制错误的示例项目: download

加载应用程序,然后更改模拟器的从风景到肖像的方向几次,它应该是oc CUR。我在物理手机上尝试过相同的代码,但行为完全相同,所以这不是模拟器相关的问题。

Load up the app and then change the Simulator's orientation a few times from Landscape to Portrait and it should occur. I have tried the same piece of code on a physical phone and it behaves in exactly the same way, so this is not a simulator related issue.

[更新]:
我已经用尽了Apple的技术团队提出的一项支持请求,看看他们是否可以帮我解决这个问题。将发布解决方案 - 如果他们有一个 - 在这里。感谢您的帮助。

[UPDATE]: I have used up one of my support requests with Apple's technical team to see if they can help me get to the bottom of this. Will post the solution - if they have one - here. Thanks for the help so far.

推荐答案

经过一周的等待,Apple开发者技术支持成功帮助我解决问题出。以下是他们的回复:

So after a week of waiting, Apple Developer Technical Support managed to help me sort my problem out. Here is their response:


我查看了你的代码,发现
a你需要关注的一些事情
左右,其中一些可能会导致
你的问题。在
ControllerSwitchAppDelegate.m
来源中,你正在实施
didRotate 方法。可能是
值得检查设备方向
通知在视图控制器
级别而不是在UIApplication
level。这将使你的代码更加简单和封装允许
每个显示为
的视图控制器处理它自己的旋转逻辑。你还
也是同时使用多个视图
控制器,即
,两个视图属性都是
被添加并在设备
旋转时删除。这不是
使用
UIKit的常用方法。想法是提供一个
视图控制器(或其查看属性)
一次并且没有父视图
控制器交换在不同的子视图
控制器中。表面上看
你的方法似乎可行,但在
长期运行中,我建议采用不同的
方法。

"I've looked over your code and found a few things you need to be concerned about, some of which may contribute to your problem. In your ControllerSwitchAppDelegate.m source, you are implementing "didRotate" method. It might be worth checking for device orientation notifications at the view controller level rather than at the UIApplication level. This will make your code much more simpler and encapsulated allowing each view controller that is shown to handle its own rotation logic. You are also using multiple view controllers at the same time, that being, both "view" properties are being added and remove when the device is rotated. This is not exactly the common approach in which to use the UIKit. The idea is to present one view controller (or its view property) at a time and not have a parent view controller swap in different sub-view controllers. Granted on the surface your approach seems doable, but in the long run, I recommend a different approach.

我们有一个名为
AlternateViews的样本,可以找到
at -
http://developer.apple.com/iphone/library/samplecode/AlternateViews/index.html

In这个样本,你需要的几乎是
。它为
给定的设备方向提供
备用视图控制器。只是
提供一个视图控制器超过
另一个使用
presentModalViewController与
过渡属性名为
modalTransitionStyle将给你
你交叉淡化影响。

In this sample, it pretty much does what you need. It provides an "alternate" view controller for a given device orientation. Is merely presents one view controller over another using "presentModalViewController" with a transition property called "modalTransitionStyle" which will give you a cross fade affect."

我最后做的是使用一个超级视图控制器来呈现和关闭视图控制器。而不是交换视图控制器并使用AppDelegate删除子视图。

What I ended up doing was using a super view controller that presented and dismissed view controllers. Rather than swapping view controllers and removing sub views using the AppDelegate.

这篇关于View Controller即使已被解除分配也会收到消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆