Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok the delegation pattern is widely used in the Cocoa framework.</p> <p>The file that parses data must have a protocol, anyone wanting callbacks form this Class must implement the methods from the protocol:</p> <pre><code> //XMLParser.h //import statements here .. @protocol XMLParserDelegate - (void) parserDidFinish:(NSArray*) theParsedData; //have as many methods as you please, didFail, doingProgress etc. @end @interface XMLParser : NSObject { id &lt;XMLParserDelegate&gt; delegate; //we don't know what type of object this will be, only that it will adhere to the XMLParserDelegate protocol. } @property(assign) id delegate; //methods @end </code></pre> <p>In the implementation of the XMLParser:</p> <pre><code>@implementation XMLParser @synthesize delegate; - (void)parserDidEndDocument:(NSXMLParser *)parser { [self.delegate parserDidFinish:dataYouCollectedArray]; //this is where it happens. } </code></pre> <p>So in your controllers interface file you says that you will adhere to the XMLParserDelegate protocol.</p> <pre><code>//MyController.h #import "XMLParser.h" @interface MyController : UIViewController &lt;XMLParserDelegate&gt; { //this is where you "promise" to implement the methods of the protocol. } </code></pre> <p>In the MyController.m file you now instantiate the XMLParser.</p> <pre><code>@implementation MyController - (void) init { XMLParser *parser = [[XMLParser alloc] init]; [parser setDelegate:self] //now the parser has a reference to this object. [parser start]; } - (void) parserDidFinish:(NSArray*) results { //now you have your results in the controller, can set it as the data source of the tableView and call tableView.reloadData; } </code></pre> <p>This is a great pattern that loosely couples the caller and responder without having them know anything other that what the protocol dictates, about each other.</p> <p>If I have a view element that is confined to its own functionality, like, say, a clock. I would have a ClockViewController and it would instantiate arm, dials, etc. They would all be linked to the clock and alert the clock controller about their actions using this pattern. This way I can use the clock arm or dial in other code as I please, as long as the object instantiating them adheres to the ClockArmDelegate protocol.</p> <p>Hope it makes sense:) it is the, Im pretty sure, most used pattern in Cocoa.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload