2016-09-30 48 views
1

这是我以前的问题转发,但由于编辑太多,我简化了示例,我认为如果我创建一个新问题而不是重新编辑问题多次。UISwitch not deococating

问题:
UISwitch对象莫名其妙地当什么也没有用它做甚至不解除分配。


PROJECT:
只有两个视图控制器。 VC1和VC2。 VC1有一个按钮来呈现VC2。 VC2包含一个按钮,用于解除其自身和自定义的属性UISwitch,UILabelUIStepper

VC1:

class VC1: UIViewController { 

    let button = UIButton() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     view.addSubview(button) 

     button.addTarget(self, action: #selector(open), for: .touchUpInside) 

     // Some auto layout (not relevant to the question) 
    } 

    func open() { present(VC2(), animated: true) } 
} 

VC2:

class VC2: UIViewController { 

    let button = UIButton() 

    let shifty = CustomSwitch() // Note: nothing has been done with this 
    let labels = CustomLabels() 
    let steppy = CustomSteppy() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     view.addSubview(button) 

     button.addTarget(self, action: #selector(close), for: .touchUpInside) 

     // Some auto layout (not relevant to the question) 
    } 

    func close() { dismiss(animated: true) } 
} 

子类:

class CustomSwitch: UISwitch { deinit { print("Switch has been deinitialized") } } 
class CustomLabels: UILabel { deinit { print("Labels has been deinitialized") } } 
class CustomSteppy: UIStepper { deinit { print("Steppy has been deinitialized") } } 

我创建这些子类的唯一原因是S o我可以在探查器中更轻松地追踪它们。即使我没有子类UISwitch也会发生同样的情况。

编辑:
我添加了一个deinit到子类和两个UILabel还有UIStepper都出现消息:

Labels has been deinitialized 
Steppy has been deinitialized 

因此,它似乎没有UISwitch越来越deinitialized。

截图:
在这个截图中,我已经打开,并多次关闭VC2。在那里你可以看到只有对象CustomSwitch一直保持不变,而CustomLabelsCustomSteppy已被释放,正如它应该。

Original state

至于建议的Rmaddy也是为什么我想创造一个新的问题,其原因是引用计数的结果。我已经在SO上进行了一些解释,但我不完全确定要做什么。

Reference counts (collapsed)

Reference counts


问题:
这是为什么UISwitch表现这样,我该如何解决这一问题?

+0

您是否按照我之前提出的问题建议?作为提醒,要检查仪器中的“记录参考计数”复选框,然后重新分析应用程序。然后看看维护每个交换机的参考。 – rmaddy

+0

@rmaddy对不起,是的,我已经做到了,我忘了添加这些截图。虽然我现在很困惑,但我不知道该怎么做。尝试和寻找修复它,但仍然没有运气。 –

+0

它显示计数为+0。这意味着它应该被释放。你是在真实的设备上还是在模拟器上测试?我不相信这种模拟器。 – rmaddy

回答

0

这是(最后)在iOS 10.2中修复的问题