iOS:如何检查UIViewControllers是否正在卸载? (迅速) [英] iOS: How to check if UIViewControllers are unloading? (Swift)

查看:84
本文介绍了iOS:如何检查UIViewControllers是否正在卸载? (迅速)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次在主VC中单击一行时,我都会使用UISplitViewController.我可以看到viewDidLoad()在Detail VC中运行.

I'm using a UISplitViewController every time I click on a row in the Master VC I can see that viewDidLoad() is run in the Detail VC.

这是否意味着我要在每行单击时创建一个Detail VC的新实例?

Does this mean i'm creating a new instance of Detail VC each row click?

如果是这样,我如何检查Detail VC是否正确卸载,而不仅仅是创建越来越多的新Detail VC?

If so, how can I check that the Detail VC are unloading correctly and that i'm not just creating more and more new Detail VCs?

在Swift中我有点迷路了.以前,我可以在dealloc()中使用NSLog登录,并看到UIViewController正确卸载.

I'm a bit lost here in Swift. Previously I could NSLog in dealloc() and see the UIViewController correctly unloading.

我这里的Swift有一个deinit函数,但是从来没有这样称呼过:

I here Swift has a deinit function but this is never called:

deinit {
    println("\(__FILE__.lastPathComponent)) : \(__FUNCTION__)")
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

1)我应该在哪里遣散我的观察员?

1) Where should I be removing my observers?

2)当我在Xcode中的调试导航器"中查看时,内存使用量一直在增加,而从未减少.

2) When I look in the Debug Navigator in Xcode the Memory usage just keeps going up and never down.

if segue.identifier == "addEvent" {
    if let controller = (segue.destinationViewController as UINavigationController).topViewController as? ManageViewController {
        controller.manageEvent = nil
        controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
        controller.navigationItem.leftItemsSupplementBackButton = true
    }
}

我做的事情与我看到的许多例子并没有什么不同,但是我担心deinit不会被调用

I'm not doing anything different than lots of examples i've seen, but I am worried about deinit not being called

我原来的无效代码是:

protocol ManageViewDelegate {
    func pressedButton(sender: AnyObject)
}

class ManageView: UIView {
    var delegate: ManageViewDelegate? = nil
    ...
}

新的工作代码:

protocol ManageViewDelegate: class {
    func pressedButton(sender: AnyObject)
}

class ManageView: UIView {
    weak var delegate: ManageViewDelegate? = nil
    ...
}

推荐答案

您有一个带有delegate属性的视图,该属性引用了视图控制器.这将导致强大的参考周期(以前称为保留周期),因为视图控制器将对其顶层视图保持强烈的引用,从而又将强大的参考返回给视图控制器.

You have a view with a delegate property that references back to the view controller. This will result in a strong reference cycle (previously known as a retain cycle) because the view controller is maintaining a strong reference to its top level view which is, in turn, maintaining a strong reference back to the view controller.

In the Resolving Strong Reference Cycles Between Class Instances section of The Swift Programming Language: Automatic Reference Counting, Apple describes how to address this:

在使用类类型的属性时,Swift提供了两种解决强引用周期的方法:弱引用和无主引用.

Swift provides two ways to resolve strong reference cycles when you work with properties of class type: weak references and unowned references.

弱引用和无所有权引用使引用循环中的一个实例可以引用另一个实例,而无需对其保持强大的控制力.然后,这些实例可以相互引用,而无需创建强大的引用周期.

Weak and unowned references enable one instance in a reference cycle to refer to the other instance without keeping a strong hold on it. The instances can then refer to each other without creating a strong reference cycle.

只要有效的引用在其生命周期中的某个时刻变为nil,请使用weak引用.相反,如果您知道一旦在初始化过程中设置了nil引用,就可以使用unowned引用.

Use a weak reference whenever it is valid for that reference to become nil at some point during its lifetime. Conversely, use an unowned reference when you know that the reference will never be nil once it has been set during initialization.

因此,您可以通过将delegate定义为weak来解决强大的参考周期:

Thus, you can resolve your strong reference cycle by defining the delegate to be weak:

weak var delegate: ManageViewDelegate? 

要使其正常工作,您必须将协议指定为类协议:

For that to work, you have to specify your protocol to be a class protocol:

protocol ManageViewDelegate: class {
    // your protocol here
}

这将解决强参考周期,而无需手动nil delegate来解决强参考周期.

That will resolve the strong reference cycle and eliminates the need to manually nil the delegate in order to resolve the strong reference cycle.

这篇关于iOS:如何检查UIViewControllers是否正在卸载? (迅速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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