From Medien Wiki

Calling a Method

There are quite different ways to call a method:

Call selector for known class:

  • [object doSomething:TRUE];
  • NSString *s = [object name];

Call selector for unknown class (type <id>*)

  • [object performSelector:@selector(doSomething:) withObject:[NSNuber numberWithBool:YES]];
  • [object performSelectorOnMainThread:@selector(doSomethingElse) withObject:nil waitUntilDone:NO];

Use KVC (Key-Value-Coding) for Properties

  • NSString *s = [object valueForKey:@"name"];
  • [object setValue:@"Hello" forKey:@"name"];

Use dot syntax (though this also works with action methods, it's recommended to use only for properties

  • NSString *s =;
  • = @"Hello";

Note that in case of KVC- or performSelector:-messaging style, you have to use objects as parameters. They won't work with primitives (such as int, BOOL...). Note, that it's not possible to overload methods in ObjC, so you have to implement both methods. Usually this is done like this, eg:

-(void)doSomething:(BOOL)value {
-(void)doSomething:(id)value {
    [self doSomething:[value boolValue]];


Sometimes there are simpler ways to get notified of or to react to certain events. For example, there are some methods in UIViewController that can be overridden by subclasses:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration;
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;

Delegation & Notification

The main difference between Delegation and Notification is, that an object usually has only one delegate. The delegate can be informed to react on changes; or it can be asked for opinion or help. Think about it like a red telephone where you can rely on getting your manager on the phone immediately to discuss important matters.

Whereas notifications are usually broadcasted into the wide open space (not necessarily restricted to the app itself!) for interested listeners. It's pretty unsure if anyone is receiving this. Also, the message is handled by the notification center. Think about it like a news mailing by post or an electronic mailing list, where you don't know who's reading it and when it's received. In fact, chances are high, most objects will be notified the next run loop.


You will find delegates all over the place. Important Delegate protocols are (just to name a few):

  • UIApplicationDelegate
  • UITableViewDelegate
  • UITextViewDelegate
  • UIAccelerometerDelegate
  • MKMapViewDelegate
  • CGLocationManagerDelegate
  • ...


Delegation is a very important design pattern with cocoa. Usually it's much simpler or cleaner to use a delegate instead of subclassing. The one delegate we are using all the time is UIApplication delegate. Classes providing delegate functions have a protocol with defines required and optional methods to be implemented by the object's delegate:

// if a class follows a delegate protocol, we add this to the header
// the only reason for this is to ensure we implement all the required methods, eg:
@interface MyAppDelegate : NSObject <UIApplicationDelegate> { }
// Next, you have to either set the delegate in Interface Builder or do so programmatically
[[UIApplication sharedApplication] setDelegate:self];
// Then you can implement following <UIApplicationDelegate> method to get informed once the app has finished launching


Notifications get broadcasted to all and everything that registered as observer to a certain notification:

// +++ The Observer +++
// usually on -init or -awakeFromNib:
[[NSNotificationCenter defaultCenter] addObserver:self
// then implement your custom method ( -(void)receiveNotification:(NSNotification*)notification; )
// and finally remove notification observers before deallocation
[[NSNotificationCenter defaultCenter] removeObserver:self];

// +++ The Broadcaster +++
// broadcast a notification to all registered objects
[[NSNotificationCenter defaultCenter] postNotificationName:@"SomethingHappenedNotification"


Diese Seite ist Teil des Werkmoduls iOS Development von Michael Markert für Interface Design / Fakultät Medien an der Bauhaus-Universität Weimar.