How do I create delegates in Objective-C?

Posted by Editorial Staff | Updated on

Want to create delegates in Objective-C? An Objective-C delegate is an object that has been assigned to the delegate property another object. To create one, you define a class that implements the delegate methods you’re interested in, and mark that class as implementing the delegate protocol.

For example, suppose you have a UIWebView. If you’d like to implement its delegate’s webViewDidStartLoad: method, you could create a class like this:

Then you could create an instance of MyClass and assign it as the web view’s delegate:

On the UIWebView side, it probably has code similar to this to see if the delegate responds to the webViewDidStartLoad: message using respondsToSelector: and send it if appropriate.

If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@] Your question and answer will appear on and help other developers.

Related Questions & Answers