Reputation: 449
there are many similar questions about TextFields delegate method textfieldshouldreturn not being called, but all were solved by setting the delegate. Ive set the delegate, and also have a perfectly fine example in another project I've copied almost line for line. A print statement confirms no call is made. Whats more curious is that I set a random variable to test if I was even accessing the right object, but when I tried to access that variable, it crashed with a BAD_ACCESS error.
class TitleTextField: UITextField, UITextFieldDelegate {
var randomElement: Bool = true
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
print("text field return pressed")
return true
}
}
and here is where I'm using it
class EditViewController: UIViewController {
@IBOutlet weak var titleTextField: TitleTextField!
func configureView() {
navigationItem.title = "Edit Goal"
}
override func viewDidLoad() {
super.viewDidLoad()
print("editor loaded")
configureView()
titleTextField.text = "placeholder"
titleTextField.delegate = titleTextField
titleTextField.delegate = titleTextField.self
if let textField = titleTextField {
textField.delegate = titleTextField
}
print("textfield delegate = \(titleTextField?.delegate)")
}
If listed some of the different ways I tried setting the delegate. I even conformed the viewController to UITextFieldDelegate and set the delegate to self but that didn't matter either. I added "randomVariable" to TitleTextField to make sure I was accessing the correct object, but when I used titleTextField.randomVariable = true in viewDidLoad, I got a BAD_ACCESS crash.
Ive also double checked the storyboard connection. I even deleted the connection and IBoutlet and redid them, no difference. cleaned project etc.
Upvotes: 0
Views: 3036
Reputation: 449
Wow ok, so the problem was I hadnt set the textfield class to TitleTextField in my identity inspector. I had it programmatically set, I guess I didnt realize i had to do it in the storyboard too.
Upvotes: 1
Reputation: 4287
The issue is that you're conforming to the UITextFieldDelegate
on your custom TitleTextField
itself. Instead, you should conform to the protocol on your UIViewController
, like so:
class EditViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var titleTextField: TitleTextField!
func configureView() {
navigationItem.title = "Edit Goal"
}
override func viewDidLoad() {
super.viewDidLoad()
print("editor loaded")
configureView()
titleTextField.text = "placeholder"
titleTextField.delegate = self
print("textfield delegate = \(titleTextField?.delegate)")
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
print("text field return pressed")
return true
}
The purpose of the delegate is to respond to editing-related messages from the text field (link to docs). This means that the UITextField
is already aware of these editing events. What you need to do is allow the class containing your custom UITextField
to listen to the events that it is sending out. In your situation, that class is EditViewController
. You can make EditViewController
listen to the UITextView
's events by setting it as the delegate.
The reason for your BAD_ACCESS error is a memory-related issue. Your UITextField
is calling itself infinitely through recursion. If you look through the calling stack you'll probably see it calling the same method hundreds of times. See this post for more insight.
Upvotes: 0