问题
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