Retain Cycle (Strong reference) fix for custom uitextfield?

陌路散爱 提交于 2019-12-02 03:11:04

问题


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.

The txt field class:

class LoginTextFields: UITextField, UITextFieldDelegate {

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

    delegate = self
    setConstraints()
  }
}

The view controller:

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

}

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()
}

回答1:


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.

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

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?



来源:https://stackoverflow.com/questions/48088704/retain-cycle-strong-reference-fix-for-custom-uitextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!