One Quickie


Making a delegate (NSObject->General)
You've decided you want your class to have a delegate. Here's how you go about doing it:
  1. Make an id instance variable in your class, and make methods to set and get it:
    id   delegate;
    ...
    -(void) setDelegate: (id) del
    {
        delegate = del; // you don't need to retain it
    } // setDelegate
    
    - (id) delegate
    {
        return (delegate);
    } // delegate
    

  2. Make a category on NSObject so the compiler won't generate warnings, and also to document the delegate methods you support:
    @interface NSObject(BWStitchViewDelegate)
    - (BOOL) shouldHandleStuff: (NSData *) data;
    @end
    

  3. Check to make sure your delegate understands the message before sending it.
    ...
    if ([delegate respondsToSelector: @selector(shouldHandleStuff:)]) {
        BOOL blah;
        blah = [delegate shouldHandleStuff: somedata];
        if (blah) ...
    }
    



borkware home | products | miniblog | rants | quickies | cocoaheads
Advanced Mac OS X Programming book

webmonster@borkware.com