Cocoa Delegate Messages

Discussion in 'Mac Programming' started by MorphingDragon, Jul 12, 2010.

  1. MorphingDragon macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #1
    How do I send a message to a class' delegate?

    like say

    [self sendMessageToDelegate:mad:selector(gameWon)];
     
  2. dws90 macrumors regular

    Joined:
    Jan 16, 2008
    #2
    There's a bit of flexibility on how you want to structure things, but the vast majority of the time, the delegate is held as an instance variable, and you just pass a message to it like you would any other object.

    So, you might declare the instance variable
    id <BigRedButtonDelegate> _delegate;

    (where BigRedButtonDelegate is a protocol you define)

    then, you just call make a call like:
    [_delegate somebodyDidPressTheBigRedButton];
     
  3. misee macrumors member

    Joined:
    Jul 4, 2010
    #3
    If you have methods that your delegate can implement optionally, you can also use informal protocols (or the @optional in a protocol interface) and then do something like this:
    Code:
    if ([delegate respondsToSelector:@selector(infoForMyClass:))
        [delegate infoForMyClass:self];
     
  4. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #4
    This question is confusing. The delegate is just an object, you message it the same as you would with any other object method pair. The only real important thing to decide is if you want to check to see that the object responds to the method, or if the object "says" it conforms to the delegate protocol.
     

Share This Page