views:

923

answers:

1

I went through this tutorial Your First iPhone Application and it worked great. My question is about the line in the tutorial that reads as follows:

The view controller is also going to be the text field’s delegate; as such, it must adopt the UITextFieldDelegate protocol. To specify that a class adopts a protocol, in the interface add the name of the protocol in angle brackets (<>) after the name of the class from which your class inherits.

If I remove the protocol from view controller the method is still called (message is received) and the keyboard is dismissed. I noticed that all the methods for the UITextFieldDelegate protocol are optional. If that's the case, why declare that the view controller adheres to the protocol? What am I missing? :-)

+4  A: 

Because programmers like to keep things organized. Even if all of the methods of a protocol are optional, it's still good form to formally declare that your class implements that protocol if it will be used as a delegate. The delegating class may also require that your class implements the protocol - meaning that if you implement the methods but don't declare that you implement the protocol, you'll get compile warnings.

pix0r
Is there a compiler switch or directive that would allow me to see situations where I've linked the UITextField with the View Controller, but haven't declared that the View Control follows the UITextFieldDelegate protocol?
James Sun