Deinit 未调用 - 找不到保留某些内容的原因(提供代码) [英] Deinit not calling - Cannot find why something is retaining (code provided)

查看:32
本文介绍了Deinit 未调用 - 找不到保留某些内容的原因(提供代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我的 UIViewcontroller 在以下情况下没有调用 deinit().我正在使用此代码扩展通过添加点击手势识别器来让我的生活更轻松.

https://gist.github.com/saoudrizwan/548aa90be174320fbaa6aa6我在我的一个 VC 中使用了此代码,我已将其精简到最少的代码量:

viewDidLoad() 中我这样做了:

//当用户点击一个标签时,它的相关文本框会自动获得插入符号,以便他们可以输入//添加点击,以便当您点击标签时,它会使相应的文本框成为第一响应者lblSubject.addTapGestureRecognizer {self.txtSubject.becomeFirstResponder()}

似乎是该行:

self.txtSubject.becomeFirstResponder()

问题是 - 当我在闭包中保留上面的这一行时,deinit() 不会在我的 VC 中调用.当我把上面的行去掉或用类似 print("hello world") 之类的东西替换它时deinit() 正确调用.txtSubject 是 @IBOutlet 弱变量 txtSubject: UITextField!

我不完全确定在这里做什么.我读到当你触发 becomeFirstResponder() 时,你调用 resignFirstResponder() 很重要,但即使我不点击标签(以免给 becomeFirstResponder() 连调用的机会)我还是打不过deinit()

有什么想法可以让我看得更远吗?

非常感谢.

更改

self.txtSubject.becomeFirstResponder()

 [unowned self] in self.txtSubject.becomeFirstResponder()

unowned 通常被认为是危险的,但这里没有危险.如果 self 不复存在,将没有任何东西可以挖掘,代码将永远不会运行.

I have discovered that my UIViewcontroller is not calling deinit() under the following scenario. I am using this code extension to make my life easier by adding tap gesture recognizers.

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 未调用 - 找不到保留某些内容的原因(提供代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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