问题描述
我发现我的 UIViewcontroller 在以下情况下没有调用 deinit()
.我正在使用此代码扩展通过添加点击手势识别器来让我的生活更轻松.
https://gist.github.com/saoudrizwan/548aa90be174320fbaa6aa6我在我的一个 VC 中使用了此代码,我已将其精简到最少的代码量:
//当用户点击一个标签时,它的相关文本框会自动获得插入符号,以便他们可以输入//添加点击,以便当您点击标签时,它会使相应的文本框成为第一响应者lblSubject.addTapGestureRecognizer {self.txtSubject.becomeFirstResponder()}
self.txtSubject.becomeFirstResponder()
self.txtSubject.becomeFirstResponder()
[unowned self] in self.txtSubject.becomeFirstResponder()
unowned
通常被认为是危险的,但这里没有危险.如果 self
不复存在,将没有任何东西可以挖掘,代码将永远不会运行.
https://gist.github.com/saoudrizwan/548aa90be174320fbaa6b3e71f01f6ae
I've used this code in one of my VCs, which I've stripped down to the barest minimum amount of code:
and in viewDidLoad()
I did this:
// When the user taps on a label, have its related textbox automatically get the caret so they can type
// Add tapping so when you tap on a label it makes the corresponding textbox first responder
lblSubject.addTapGestureRecognizer {
self.txtSubject.becomeFirstResponder()
}
It appears that the line:
self.txtSubject.becomeFirstResponder()
Is the problem - when I leave this line above in that closure, deinit()
does not call in my VC.When I take the above line out or replace it with something like print("hello world")
deinit()
properly calls. txtSubject is @IBOutlet weak var txtSubject: UITextField!
I am not entirely sure what to do here. I read that when you trigger becomeFirstResponder()
it's important you call resignFirstResponder()
, but even if I don't tap the label (so as to not give becomeFirstResponder()
a chance to even call) I still cannot hit deinit()
Any ideas where I can look further?
Thanks so much.
Change
self.txtSubject.becomeFirstResponder()
To
[unowned self] in self.txtSubject.becomeFirstResponder()
unowned
is often feared as dangerous, but there is no danger here. If self
ceases to exist, there will be nothing to tap and the code will never run.
这篇关于Deinit 未调用 - 找不到保留某些内容的原因(提供代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!