iosdelegatesuitextfieldobjective-c-protocol

Why do delegate methods work without declaring protocol conformance?


I have one UITextField, and I drag from it to its owner to set the delegate in storyboard.

And in my controller.h file, I didn't declare the class as conforming to the UITextFieldDelegate protocol.

But the - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string works! It just works!

And I wonder why?

I have met this once before when I set UITableView to a controller which does not have UITableViewDelegate and UITableViewDataSource.


Solution

  • The methods are invoked because the delegating objects (UIWebView, UITextField, UITableView, etc.) don't check if the delegate conforms to a protocol. They are only interested in if it actually responds to a certain delegate method.

    Protocol declarations, after all, are just hints for the compiler.