{"id":26921,"date":"2022-12-20T20:45:16","date_gmt":"2022-12-20T15:15:16","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/"},"modified":"2022-12-20T20:45:16","modified_gmt":"2022-12-20T15:15:16","slug":"solved-how-do-i-create-delegates-in-objective-c","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/","title":{"rendered":"[Solved] How do I create delegates in Objective-C?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-626946\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"626946\" data-parentid=\"626898\" data-score=\"918\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>An Objective-C delegate is an object that has been assigned to the <code>delegate<\/code> property another object. To create one, you define a class that implements the delegate methods you&#8217;re interested in, and mark that class as implementing the delegate protocol.<\/p>\n<p>For example, suppose you have a <code>UIWebView<\/code>. If you&#8217;d like to implement its delegate&#8217;s <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/UIKit\/Reference\/UIWebViewDelegate_Protocol\/index.html#\/\/apple_ref\/occ\/intfm\/UIWebViewDelegate\/webViewDidStartLoad:\"><code>webViewDidStartLoad:<\/code><\/a> method, you could create a class like this:<\/p>\n<pre><code>@interface MyClass&lt;UIWebViewDelegate&gt;\n\/\/ ...\n@end\n\n@implementation MyClass\n- (void)webViewDidStartLoad:(UIWebView *)webView { \n    \/\/ ... \n}\n@end\n<\/code><\/pre>\n<p>Then you could create an instance of MyClass and assign it as the web view&#8217;s delegate:<\/p>\n<pre><code>MyClass *instanceOfMyClass = [[MyClass alloc] init];\nmyWebView.delegate = instanceOfMyClass;\n<\/code><\/pre>\n<p>On the <code>UIWebView<\/code> side, it probably has code similar to this to see if the delegate responds to the <code>webViewDidStartLoad:<\/code> message using <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/developer.apple.com\/documentation\/Cocoa\/Reference\/Foundation\/Protocols\/NSObject_Protocol\/Reference\/NSObject.html#\/\/apple_ref\/occ\/intfm\/NSObject\/respondsToSelector:\"><code>respondsToSelector:<\/code><\/a> and send it if appropriate.<\/p>\n<pre><code>if([self.delegate respondsToSelector:@selector(webViewDidStartLoad:)]) {\n    [self.delegate webViewDidStartLoad:self];\n}\n<\/code><\/pre>\n<p>The delegate property itself is typically declared <code>weak<\/code> (in ARC) or <code>assign<\/code> (pre-ARC) to avoid retain loops, since the delegate of an object often holds a strong reference to that object. (For example, a view controller is often the delegate of a view it contains.)<\/p>\n<h2>Making Delegates for Your Classes<\/h2>\n<p>To define your own delegates, you&#8217;ll have to declare their methods somewhere, as discussed in the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.google.ca\/search?q=site:developer.apple.com+protocols+objective+c&amp;btnI\">Apple Docs on protocols<\/a>. You usually declare a formal protocol. The declaration, paraphrased from UIWebView.h, would look like this:<\/p>\n<pre><code>@protocol UIWebViewDelegate &lt;NSObject&gt;\n@optional\n- (void)webViewDidStartLoad:(UIWebView *)webView;\n\/\/ ... other methods here\n@end\n<\/code><\/pre>\n<p>This is analogous to an interface or abstract base class, as it creates a special type for your delegate, <code>UIWebViewDelegate<\/code> in this case. Delegate implementors would have to adopt this protocol:<\/p>\n<pre><code>@interface MyClass &lt;UIWebViewDelegate&gt;\n\/\/ ...\n@end\n<\/code><\/pre>\n<p>And then implement the methods in the protocol. For methods declared in the protocol as <code>@optional<\/code> (like most delegate methods), you need to check with <code>-respondsToSelector:<\/code> before calling a particular method on it. <\/p>\n<h3>Naming<\/h3>\n<p>Delegate methods are typically named starting with the delegating class name, and take the delegating object as the first parameter. They also often use a will-, should-, or did- form. So, <code>webViewDidStartLoad:<\/code> (first parameter is the web view) rather than <code>loadStarted<\/code> (taking no parameters) for example. <\/p>\n<h3>Speed Optimizations<\/h3>\n<p>Instead of checking whether a delegate responds to a selector every time we want to message it, you can cache that information when delegates are set. One very clean way to do this is to use a bitfield, as follows:<\/p>\n<pre><code>@protocol SomethingDelegate &lt;NSObject&gt;\n@optional\n- (void)something:(id)something didFinishLoadingItem:(id)item;\n- (void)something:(id)something didFailWithError:(NSError *)error;\n@end\n\n@interface Something : NSObject\n@property (nonatomic, weak) id &lt;SomethingDelegate&gt; delegate;\n@end\n\n@implementation Something {\n  struct {\n    unsigned int didFinishLoadingItem:1;\n    unsigned int didFailWithError:1;\n  } delegateRespondsTo;\n}\n@synthesize delegate;\n\n- (void)setDelegate:(id &lt;SomethingDelegate&gt;)aDelegate {\n  if (delegate != aDelegate) {\n    delegate = aDelegate;\n\n    delegateRespondsTo.didFinishLoadingItem = [delegate respondsToSelector:@selector(something:didFinishLoadingItem:)];\n    delegateRespondsTo.didFailWithError = [delegate respondsToSelector:@selector(something:didFailWithError:)];\n  }\n}\n@end\n<\/code><\/pre>\n<p>Then, in the body, we can check that our delegate handles messages by accessing our <code>delegateRespondsTo<\/code> struct, rather than by sending <code>-respondsToSelector:<\/code> over and over again.<\/p>\n<h3>Informal Delegates<\/h3>\n<p>Before protocols existed, it was common to use a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/www.google.ca\/search?q=site:developer.apple.com+category+objective+c&amp;btnI\">category<\/a> on <code>NSObject<\/code> to declare the methods a delegate could implement. For example, <code>CALayer<\/code> still does this:<\/p>\n<pre><code>@interface NSObject(CALayerDelegate)\n- (void)displayLayer:(CALayer *)layer;\n\/\/ ... other methods here\n@end\n<\/code><\/pre>\n<p>This tells the compiler that any object might implement <code>displayLayer:<\/code>.<\/p>\n<p>You would then use the same <code>-respondsToSelector:<\/code> approach as described above to call this method. Delegates implement this method and assign the <code>delegate<\/code> property, and that&#8217;s it (there&#8217;s no declaring you conform to a protocol). This method is common in Apple&#8217;s libraries, but new code should use the more modern protocol approach above, since this approach pollutes <code>NSObject<\/code> (which makes autocomplete less useful) and makes it hard for the compiler to warn you about typos and similar errors.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">4<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How do I create delegates in Objective-C? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] 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&#8217;re interested in, and mark that class as implementing the delegate protocol. For example, suppose you have a UIWebView. If you&#8217;d like to implement its delegate&#8217;s &#8230; <a title=\"[Solved] How do I create delegates in Objective-C?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/\" aria-label=\"More on [Solved] How do I create delegates in Objective-C?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[396,931,1239,471,470],"class_list":["post-26921","post","type-post","status-publish","format-standard","hentry","category-solved","tag-callback","tag-cocoa","tag-delegates","tag-ios","tag-objective-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How do I create delegates in Objective-C? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How do I create delegates in Objective-C? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] 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&#8217;re interested in, and mark that class as implementing the delegate protocol. For example, suppose you have a UIWebView. If you&#8217;d like to implement its delegate&#8217;s ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-20T15:15:16+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How do I create delegates in Objective-C?\",\"datePublished\":\"2022-12-20T15:15:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/\"},\"wordCount\":493,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"callback\",\"cocoa\",\"delegates\",\"ios\",\"objective-c\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/\",\"name\":\"[Solved] How do I create delegates in Objective-C? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-20T15:15:16+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How do I create delegates in Objective-C?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How do I create delegates in Objective-C? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How do I create delegates in Objective-C? - JassWeb","og_description":"[ad_1] 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&#8217;re interested in, and mark that class as implementing the delegate protocol. For example, suppose you have a UIWebView. If you&#8217;d like to implement its delegate&#8217;s ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/","og_site_name":"JassWeb","article_published_time":"2022-12-20T15:15:16+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How do I create delegates in Objective-C?","datePublished":"2022-12-20T15:15:16+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/"},"wordCount":493,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["callback","cocoa","delegates","ios","objective-c"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/","url":"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/","name":"[Solved] How do I create delegates in Objective-C? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-20T15:15:16+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-do-i-create-delegates-in-objective-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How do I create delegates in Objective-C?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/26921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=26921"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/26921\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=26921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=26921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=26921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}