UISwitch 不释放 [英] UISwitch not deallocating
问题描述
这是我之前问题的转贴,但由于编辑太多,我简化了示例,我认为如果我创建一个新问题而不是多次重新编辑问题会更清楚.
This is a repost of my question before but because of too many edit's and I've simplified my examples, I thought it would be more clear if I create a new question instead of re-edit the questions multiple times.
问题:UISwitch
对象不知何故不会解除分配,即使没有对其进行任何处理.
PROBLEM:
UISwitch
object somehow doesn't deallocate even when nothing has been done with it.
项目:
只有两个视图控制器.VC1 和 VC2.VC1 有一个按钮来呈现 VC2.VC2 包含一个关闭自身的按钮和一个自定义 UISwitch
、UILabel
和一个 UIStepper
的属性.
PROJECT:
There are only two view controllers. VC1 and VC2. VC1 has one button to present VC2. VC2 contains one button to dismiss itself and a property of a custom UISwitch
, UILabel
and a UIStepper
.
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") } }
我创建这些子类的唯一原因是我可以在分析器中更轻松地跟踪它们.即使我没有子类化 UISwitch
,也会发生同样的情况.
The only reason I created these subclasses is so I could track them easier in the profiler. The same happens even when I don't subclass UISwitch
.
我在子类中添加了 deinit
,UILabel
和 UIStepper
都显示了消息:
I've added a deinit
to the subclasses and both the UILabel
as well as the UIStepper
are showing the message:
Labels has been deinitialized
Steppy has been deinitialized
所以 UISwitch
似乎没有被取消初始化.
So it doesn't seem the UISwitch
is getting deinitialized.
屏幕截图:
在此屏幕截图中,我多次打开和关闭 VC2.在那里,您只能看到对象 CustomSwitch
保持不变,而 CustomLabels
和 CustomSteppy
已按原样解除分配.
SCREENSHOT:
In this screenshot, I've opened and closed VC2 multiple times. In there you can see only the object CustomSwitch
has stayed persistent while CustomLabels
and CustomSteppy
has been deallocated just as it should be.
正如 Rmaddy 所建议的,也是我想创建一个新问题的原因是引用计数的结果.我已经遵循了一些关于 SO 的解释,但我不太确定要完全理解它.
As suggested by Rmaddy and also the reason why I wanted to create a new question is the result of the reference counts. I've followed some explanations on SO but I'm not very sure what to make out of it entirely.
问题:
为什么这个 UISwitch 的行为是这样的,我该如何解决这个问题?
QUESTION:
Why is this UISwitch behaving like this and how can I fix this?
推荐答案
这在 iOS 10.2 中(最终)修复
This is (finally) fixed in iOS 10.2
这篇关于UISwitch 不释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!