Deinit没有打电话给Show Detail segue [英] Deinit not called on Show Detail segue

查看:123
本文介绍了Deinit没有打电话给Show Detail segue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个更大的Swift项目中遇到了这个问题,但是能够用一个非常基本的项目复制它。我有以下视图控制器类:

I encountered this issue on a larger Swift project, but was able to replicate it with a very basic project. I have the following view controller class:

class ViewController: UIViewController {
    deinit {
        println("Deinitializing")
    }

    override func viewDidAppear(animated: Bool) {
        NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "segue", userInfo: nil, repeats: false)
    }

    func segue() {
        self.performSegueWithIdentifier("segue", sender: self)
    }
}

我的故事板配置为有两个视图控制器,都使用上面的类和显示详细信息 segues指向彼此。我可以确认它们来回反弹。尽管每次都显示一个新实例,并且没有保留周期,但不会调用先前显示的视图控制器的 deinit 方法。

My storyboard is configured so there are two view controllers, both using the above class and with Show Detail segues pointing to one another. I can confirm that they "bounce" back and forth. Despite a new instance being shown every time, and no retain cycles, the previously displayed view controller's deinit method is not called.

我已经能够通过使用自定义segue获得我想要的行为,但我的问题是:是否需要这种行为?

I've been able to get my desired behaviour through the use of a custom segue, but my question is: Is this behaviour expected?

文档似乎表明它应该按照我的预期工作,我突出了我认为适用于我的测试项目的部分。

The documentation seems to indicate that it should work how I expect it, I've highlighted the part that I think applies to my test project.

来源

在详细区域中显示内容。如果应用程序显示主视图和详细视图,则新内容将替换当前详细信息。 如果应用程序仅显示主数据或详细信息,则内容将替换当前视图控制器堆栈的顶部。

我原以为替换当前视图控制器堆栈的顶部会导致当前视图控制器被取消初始化。

I would have expected that replacing the top of the current view controller stack would cause the current view controller to be deinitialized.

作为参考,我能够通过这个segue实现我想要的行为(它没有做任何花哨的事情,可能是不正确的):

For reference, I was able to achieve my desired behaviour through this segue (which doesn't do anything fancy, and may be incorrect):

class ExampleSegue: UIStoryboardSegue {
    override func perform() {
        let source = self.sourceViewController as UIViewController
        let destination = self.destinationViewController as UIViewController

        source.view.window?.rootViewController = destination
    }
}

感谢您的帮助!

推荐答案

如果您没有使用导航控件ler或拆分视图控制器,然后显示细节segue与模态表示相同。一个呈现另一个的控制器在其presentViewController属性中保留对它的引用(并且呈现的控制器在其presentViewController属性中引用了该呈现器)。因此,没有一个控制器会被释放。如果要返回上一个控制器,则应使用展开segue或在代码中使用dismissViewControllerAnimated:completion。

If you're not using a navigation controller or a split view controller, then a "Show Detail" segue is the same as a modal presentation. A controller that presents another keeps a reference to it in its presentedViewController property (and the presented controller has a reference to the presenter in its presentingViewController property). Thus, none of the controllers will ever be deallocated. If you want to go back to the previous controller, you should either use an unwind segue or use dismissViewControllerAnimated:completion in code.

这篇关于Deinit没有打电话给Show Detail segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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