Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A delegate is an object that will respond to pre-chosen selectors (function calls) at some point in the future.</p> <p>Suppose I am loading a URL asynchronously (in the background), using an object of class FancyAsynchronousURLGetter. Since it is running in the background, I want to be able to go and do something else while the url is loading, and then be notified when it's ready. By using a delegate on the FancyAsynchronousURLGetter and writing the appropriate code, I can specify an object with a particular selector that will be called when the FancyAsynchronousURLGetter is finished. Something like this:</p> <pre><code>- (void)loadView { ... FancyAsynchronousURLGetter* getter = [[FancyAsynchronousURLGetter alloc] initWithURL:url]; [getter setDelegate:self]; /* getter will call either - (void)fancyAsynchronousURLGetterLoadSucceeded:(FancyAsynchronousURLGetter*)g or - (void)fancyAsynchronousURLGetterLoadFailed:(FancyAsynchronousURLGetter*)g on its delegate, depending on whether load succeeded or failed */ [getter start]; ... } - (void)fancyAsynchronousURLGetterLoadSucceeded:(FancyAsynchronousURLGetter*)g { NSLog(@"Load succeeded."); } - (void)fancyAsynchronousURLGetterLoadFailed:(FancyAsynchronousURLGetter*)g { NSLog(@"Load failed."); } </code></pre> <p>and in the FancyAsynchronousURLGetter itself:</p> <pre><code>- (void)start { [self performSelectorInBackground:@selector(fetchURL) withObject:nil]; } - (void))fetchURL { Fetch the URL synchronously if ( success ) [delegate fancyAsynchronousURLGetterLoadSucceeded:self]; // note: probably want to call this on the main thread else [delegate fancyAsynchronousURLGetterLoadFailed:self]; } </code></pre>
    singulars
    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. 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