Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Why use delegates?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="xstep" data-source="post: 1052606" data-attributes="member: 11647"><p>What you have set up is a delegate. Just to note, the delegate ivar is set as type 'id'.</p><p></p><p>One reason to setup the protocol is so that compiler doesn't spit out warnings that the method call on the delegate isn't found. Another reason is so that your object doesn't have to know any further details about the delegate; you don't need to import the .h file from the delegate to clear those warnings. This allows many different objects to be a delegate to your object. Only one at a time though.</p><p></p><p>Say I have an object that contains coordinate data used to draw a map of the world. It does't know how to draw though. I also have a window where I want to draw a view and a choice between a Cylindrical Projection map and a Interrupted Projection map. These maps draw very differently, but can use the same data. The window may set up the appropriate view and set the delegate of the data to that view and then send a draw message to the data object. Since the data has a delegate that knows how to draw it will send a "drawWithData: sender" message to the map so that map will know where to get the data. It seems a bit convoluted, but it allows me to use many different map types and easily swap them out at run time.</p><p></p><p>If you wanted two maps up at the same time using the same data, then you would look at NSNotification.</p></blockquote><p></p>
[QUOTE="xstep, post: 1052606, member: 11647"] What you have set up is a delegate. Just to note, the delegate ivar is set as type 'id'. One reason to setup the protocol is so that compiler doesn't spit out warnings that the method call on the delegate isn't found. Another reason is so that your object doesn't have to know any further details about the delegate; you don't need to import the .h file from the delegate to clear those warnings. This allows many different objects to be a delegate to your object. Only one at a time though. Say I have an object that contains coordinate data used to draw a map of the world. It does't know how to draw though. I also have a window where I want to draw a view and a choice between a Cylindrical Projection map and a Interrupted Projection map. These maps draw very differently, but can use the same data. The window may set up the appropriate view and set the delegate of the data to that view and then send a draw message to the data object. Since the data has a delegate that knows how to draw it will send a "drawWithData: sender" message to the map so that map will know where to get the data. It seems a bit convoluted, but it allows me to use many different map types and easily swap them out at run time. If you wanted two maps up at the same time using the same data, then you would look at NSNotification. [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Why use delegates?
Top