强引用和UIView内存问题

时间:2016-10-27 19:43:18

标签: ios swift memory-management weak-references dealloc

我正在处理一些解除分配问题以及可能无法弄清楚的强大或循环引用。我有三个UIView实例化如下:

我在故事板中添加了一个ViewControllerUIViewUIView在类中有一个weak出口,如:

class ViewController : UIViewController {

    //MARK: - outlets
    @IBOutlet weak var firstView: FirstUiview!

} 

第二个UIView作为子视图以编程方式添加到第一个视图中,如:

class FirstUiview : UIView { 

        //creating an instance of secondUiView 
        lazy var mySecondView: SecondViewClass = {
          let dv = SecondViewClass()
          dv.backgroundColor = UIColor.red
          return dv
        }()


        //sometime later by clicking on a button 
        self.addSubview(mySecondView)

        //a button will be tapped to remove mySecondView; 
        //later will be called at some point upon tapping:

       func removingSecondViewByTapping()  {
         if mySecondView.isDescendant(of: self) {
           mySecondView.removeFromSuperview()
        }
       }

} 

现在SecondViewClass是:

class SecondViewClass : UIView { 

      //in this class I create bunch of uiview objects like below: 
      lazy var aView : UIView = {
        let hl = UIView()
        hl.tag = 0
        hl.backgroundColor = UIColor.lightGray
        return hl
      }()

      self.addSubview(aView) //... this goes on and I add other similar views the same way.

        //creating an instance of thirdView
        var let thirdView = UIView() 
        self.addSubview(thirdView)

} 

现在,如果用户点击按钮删除mySecondView,然后在其他时间再次添加(仍然在同一个ViewController中),我希望mySecondView的所有子视图都已被删除释放,但他们都在那里。如果有人能指出我在哪里,我保持强烈的参考或者是否存在循环引用问题,我将非常感激?或者别的什么?

1 个答案:

答案 0 :(得分:4)

您有两个对视图,自定义属性和调用addSubview时建立的视图层次结构参考的强引用。从视图层次结构中删除视图时,您的类本身仍然具有对它的强引用。

您可以通过将引用设为可选来解决此问题,当您致电removeFromSuperview时,也可以手动将引用设置为nil。或者,也许更容易,您可以通过使用weak引用来解决此问题,让视图层次结构为您维护强引用。由于您的自定义属性为weak,因此当您从视图层次结构中删除它时(从而消除了对它的唯一强引用),您的weak引用将自动变为nil

class FirstView: UIView {

    weak var secondView: SecondView?       // note the `weak` reference, which is obviously an optional

    //sometime later by clicking on a button

    func doSomething() {
        let subview = SecondView()
        subview.backgroundColor = .red
        self.addSubview(subview)
        secondView = subview
    }

    // a button will be tapped to remove secondView;
    // later will be called at some point upon tapping ...

    func removingSecondViewByTapping()  {
        secondView?.removeFromSuperview()
    }
}