Cocoa Delegate Messages

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

  1. MorphingDragon macrumors 603


    Mar 27, 2009
    The World Inbetween
    How do I send a message to a class' delegate?

    like say

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

    Jan 16, 2008
    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

    Jul 4, 2010
    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:
    if ([delegate respondsToSelector:@selector(infoForMyClass:))
        [delegate infoForMyClass:self];
  4. jared_kipe macrumors 68030


    Dec 8, 2003
    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