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?
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!
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?
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?
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!
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.