Your Second Swift 4 & iOS 11 App - Part 30: | Ray Wenderlich

Understanding the responder chain is critical for working with text fields. You'll get the responder chain to work in this video.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4284-your-second-swift-4-ios-11-app/lessons/30

This topic was automatically closed after 166 days. New replies are no longer allowed.

I guess I’m missing some basic understanding here, but…

Why does the textFieldShouldReturn function run even though it’s not being called anywhere? And, when it runs, how does it know what to use for its parameter?

So the line of code is:

func textFieldShouldReturn(_ textField: UITextField) → Bool {
textField.resignFirstResponder()
return false

 }

How does this line get run and how does the compiler know the parameters?

What am I missing here?

Thanks in advance,

Mary

Hi Mary, textFieldShould return is called everytime the user taps on the return key of the keyboard. The textfield automatically does this for us. By setting the view controller as the delegate of the text field, the view controller gets notified every time the user taps the return key.

When the user taps the return key, the text field calls textFieldShouldReturn on the delegate (our view controller). You’ll see that the method is passed in a text field. This parameter is just filled with the text field who called it.

If that doesn’t make sense, let me know and I’ll try to clarify. Cheers!

Hi.

Thanks for the reply…!

Ok, that helps…!

But if textFieldShouldReturn is called every time the return key is tapped, why don’t we have to add in ‘override’ before the function like we do with viewDidLoad and viewWillAppear?

Thanks again for all of your help!

Mary

And, if you don’t mind me asking, how do you know that textFieldShould is called when the user taps the return key?

I have found the Apple documentation not all that helpful. It just feels like I’m going in circles when I try to find something there. But I’m guessing that’s where I should go for that kind of info. Is that true? Where in the documentation would I find that kinda stuff?

Thanks so much!

Hi Mary,

The documentation is definitely the best place to look. (Believe it or not, it’s lightyears ahead of Microsoft’s developer documentation).

Check out the textFieldShouldReturn page (Apple Developer Documentation) and the information you are looking for is in the Discussion section. Often times when looking up information for a particular method or property, I’ll jump down to the discussion section as opposed to reading the entire page as that typically contains the information I’m looking for.

In time, you’ll use the documentation less and less, but it’s always good to check it out from time to time in order to challenge your own assumptions about the API. You may think that you understand why a method is behaving in a certain manner, but a quick check in the documentation never hurts. I hope that helps!

Hi Mary,

The reason we don’t use an override keyword is that the method is defined in a protocol. When implementing protocols, we straight out just implement the method. If the method is defined in a parent class, then we override it.

A tableview controller actually implements the tableview delegate and datasource methods, so we override those methods since we are subclassing the tableview controller. If we were to implement those methods ourselves, we would omit the override keyword.

I hope that helps!

Brian,

Thank you so much for all of your help! It’s much appreciated!

Mary

1 Like