消息在转到后台时发送到Deallocated实例

时间:2013-10-21 02:52:01

标签: iphone objective-c memory-management uiviewcontroller automatic-ref-counting

我正在使用ARC并在iOS7(iPhone 5S)上进行测试。

我有一个带有UIViewController(b)的导航控制器(a)。 b通过手动segue呈现模态NavigationViewController(c)。 c然后按下UIViewController(d)。

所以现在我们有2个导航堆栈,一个根和一个模态。

每当我去后台时,我想解雇模态NavigationViewController(c)及其所有子节点。

在UIViewController(b)中,我有以下代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(enteredBackground:)
                                             name:UIApplicationDidEnterBackgroundNotification object:nil];

}
-(void)enteredBackground:(NSNotification *)notification{
    if (self.presentedViewController) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

如果我在进入后台时使用C的rootViewController,但是当我在D上时崩溃,这可以正常工作。

2013-10-20 22:49:49.008 MyApp[2596:60b] *** -[UIView release]: message sent to deallocated instance 0x17036aa40

我该如何解决这个问题?似乎模态导航堆栈中的视图正在发送dealloc并且已经发布。查看调试器后,模态nvaigationcontroller(c)本身似乎仍然存在。

1 个答案:

答案 0 :(得分:0)

我已经制作了一个测试样本并检查了相同的场景,它在任何地方都没有崩溃。请检查ViewController C和D中UIView类型对象的任何版本。或者,您可以将代码发布到您正在执行上述所有内容的位置。