16

I am looking for good example code for using delegate and events in objective c? i am familiar with delegate and events in C#.

so if someone can help me with few lines of code it will be very helpful.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
amir
  • 171
  • 1
  • 3
  • 2
    Remember to click "accept" as soon as you are happy with an answer. – Deestan Jun 07 '10 at 13:16
  • 1
    Just a warning: delegates and events are totally different concepts in Objective-C vs C#. Objective-C doesn't have language-level events and so uses the delegate pattern to (poorly) mimic them. Delegates in C# are actually more like blocks in Objective-C. – devios1 Mar 22 '13 at 19:36

2 Answers2

22

Selectors (equivalent of C# delegates)

Beware, C# has a term called "delegate" and Objective-C has a term called "delegate", but the two have hardly anything in common.

The C# "delegate" is essentially a type-safe function pointer. The equivalent of a function pointer in Objective-C is called a "selector".

To declare a parameter, member variable or local variable to be a selector, declare the type as SEL. For instance in the header file for the NSTimer class you can find this method:

- (id)initWithFireDate:(NSDate *)date
              interval:(NSTimeInterval)seconds
                target:(id)target
              selector:(SEL)aSelector
              userInfo:(id)userInfo
               repeats:(BOOL)repeats;

This means you're meant to pass a selector as the fourth argument when using this method. You can call it like this:

[[NSTimer alloc] initWithFireDate: someDate
                         interval: someInterval
                           target: self
                         selector: @selector(myTimerCallback:)
                         userInfo: nil
                          repeats: NO];

As you can see, by writing @selector(some-name-here), I can construct a new selector (similar to how I can construct a new string by writing @"some text here"). Objective-C methods have "holes" in them where arguments are inserted, and these holes are preceded by colons. When writing a selector as above, you keep the colons but you remove all else. For instance you can write something like @selector(firstPart:secondPart:thirdPart:).

The documentation of the method that accepts a selector should usually state what sort of signature it is allowed to have. The compiler will NOT enforce this for you (this is very different from C# delegates).

Notice also that the method above asks for a "target" parameter. This is typically the object the selector will be called on. Notice that the target is the completely untyped id. The compiler does not try to enforce that the object you pass in as target actually will respond to the method indicated by the selector. If it doesn't respond, then that is a runtime error. This is part of the dynamic nature of Objective-C.

(The Objective-C "delegate" concept is really just the delegate pattern (look it up), which is very prevalent in Objective-C, often used where other langauges would use inheritance.)

Events and Actions

Regarding events, there is an NSEvent class, but I have not yet had any experience with it. It seems to be for fairly low-level handling of GUI events. The C# use for events is probably more akin to "actions" in Objective-C.

Typically, a GUI component such a button has an "action" and a "target" associated with it. You can set these either in code or in Interface Builder. The target is as explained above -- an object on which a method will be called. And the "action" is in fact just a selector.

Please read the section "The Target-Action Mechanism" of this Cocoa Fundamentals article in the Apple docs. In fact that whole page is relevant to both parts of your question, so I recommend it highly.

harms
  • 8,728
  • 4
  • 30
  • 27
  • 1
    Harms: Your explanation was excellent. I have a similar question but with the MSImageView. See if you can help. http://stackoverflow.com/questions/5938270/mouse-events-handlers-from-another-class – user523234 May 11 '11 at 01:28
  • The explanation is good for people that want to understand how delegates are created and passed to methods, but I still don't understand how I would go about calling such a delegate passed to my method. – Guss Jan 19 '12 at 17:05
  • Great explanation on how to create and pass a selector. See: http://stackoverflow.com/questions/1018195/objective-c-calling-selectors-with-multiple-arguments for information on how to call them. – Symmetry Jul 27 '12 at 11:15
  • You explained what the equivalent of a C# Delegate is in Objective C, but you didn't explain what an Objective C Delegate is? (and what the equivalent might be in C#?) – Brett Aug 11 '12 at 00:17
1

The above answer is certainly correct. However, if you are looking for a way to implement the publisher/subscriber pattern then you should check out the NSNotificationCenter. This post has a good example.

Taylor Lafrinere
  • 3,084
  • 18
  • 27