Deinit not called on Show Detail segue

两盒软妹~` 提交于 2019-12-24 11:22:44

问题


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)
    }
}

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.

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.

Source

Present the content in the detail area. If the app is displaying a master and detail view, the new content replaces the current detail. If the app is only displaying the master or the detail, the content replaces the top of the current view controller stack.

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

Alternative to Show Detail

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
    }
}

Thank you for any assistance!


回答1:


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.



来源:https://stackoverflow.com/questions/27913934/deinit-not-called-on-show-detail-segue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!