自定义 uitextfield 的保留周期(强参考)修复? [英] Retain Cycle (Strong reference) fix for custom uitextfield?

查看:19
本文介绍了自定义 uitextfield 的保留周期(强参考)修复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义文本字段类loginTextFields".我最近注意到我使用这些文本字段的视图控制器没有被释放,我怀疑它与这些文本字段有关......非常感谢任何指导:) 我已经查了很多stackoverflow 帖子,但没有一个真正帮助我.

I have a custom text field class, "loginTextFields". I recently noticed that The view controllers in which I use these text fields are not being deallocated, and I suspect that it has something to do with these text fields... Any guidance is greatly appreciated :) I've looked up quite a few stackoverflow posts but none really helped me out.

txt字段类:

class LoginTextFields: UITextField, UITextFieldDelegate {

 override init(frame: CGRect) {
    super.init(frame: frame)

    delegate = self
    setConstraints()
  }
}

视图控制器:

class LoginController: UIViewController {
  @IBOutlet weak var usernameTextField: LoginTextFields!
  @IBOutlet weak var passwordTextField: LoginTextFields!

}

我猜到并在 loginTextfield 类中添加了一个删除"方法,试图在用户退出视图控制器时将所有内容设置为 nil(怀疑委托可能与它没有解除分配有关).我希望这会删除任何强引用,但它没有工作

I've guessed and added a 'delete' method to the loginTextfield class in an attempt to set everything to nil (was suspecting maybe the delegate had something to do with it not deallocating) when the user segues out of the view controller. I was hoping this would remove any strong reference but it didn't work

  func delete() {
    self.delegate = nil
    self.text = nil
    self.leftSideIcon.removeFromSuperview() //small icon inside text field
    self.removeFromSuperview()
}

推荐答案

iOS 11 的 UITextField 中有一个已知的保留循环,阻止它被释放.查看 Apple 开发者论坛上的此主题 进行分析.

There is a known retain cycle in UITextField in iOS 11 that prevents it from being deallocated. See this thread on Apple's developer forums for an analysis.

该错误应该在 iOS 11.2.5 中修复,根据这条推文.

The bug should be fixed in iOS 11.2.5, according to this tweet.

如果您的文本字段具有对视图控制器的强引用,则可能会导致您的问题.您能否在现在可用的 iOS 11.2.5 beta 3 上进行测试,看看它是否能解决您的问题?

If your text field has a strong reference back to the view controller, that could cause your problem. Can you test on the iOS 11.2.5 beta 3 available now and see if it fixes your problem?

这篇关于自定义 uitextfield 的保留周期(强参考)修复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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