The app crashes on the line where I set the TextField delegate:
listNameTextField = self
in the viewDidLoad
method
Gives error
'NSInvalidArgumentException', reason: '-[UIView setDelegate:]: unrecognized selector sent to instance
I have already tried to make the listNameTextField
variable strong
over weak
reference but it does not change the problem
import UIKit
class CreateListViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var listNameTextField: UITextField!
var listNameString: String = ""
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Create List"
listNameTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func submitListName(_ sender: UIButton) {
if listNameString != "" {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ListView")
self.present(controller, animated: true, completion: nil)
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
if let text = listNameTextField.text {
listNameString = text
}
}
}
According to your error, your listNameTextField
is just UIView
.
Check connections in your xib
/storyboard
.
You can't change type of UIView
in storyboard to UITextField
, since storyboard generate additional properties in xml for UITextField
.
Instead you need to delete your original UIView
from storyboard and add new UITextField
and make new connection.