I want to implement UITextFieldDelegate in a class other than UIViewController , but when I do this, I get an EXC_BAD_ACCESS exception at runtime.
So why does this work:
class MyViewController : UIViewController, UITextFieldDelegate { ... func createUI() { let someTextField: UITextField = UITextField() someTextField.delegate = self ... } func textFieldShouldReturn(textField: UITextField!) -> Bool { textField.resignFirstResponder() return true; } }
But this is not so:
class MyViewController : UIViewController { ... func createUI() { let someTextField: UITextField = UITextField() someTextField.delegate = MyTextFieldDelegate() ... } } class MyTextFieldDelegate : NSObject, UITextFieldDelegate { func textFieldShouldReturn(textField: UITextField!) -> Bool { textField.resignFirstResponder() return true; } }
source share