Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
837 views
in Technique[技术] by (71.8m points)

ios - Retain Cycle (Strong reference) fix for custom uitextfield?

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()
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...