I use NSNotificationCenter , which is fantastic for this kind of work. Think of it as an easy way to send messages.
Each ViewController that you want to receive a message tells NSNotificationCenter by default that it wants to listen to your message, and when you send it, a delegate is executed in each connected listening device. For example,
ViewController.m
NSNotificationCenter *note = [NSNotificationCenter defaultCenter]; [note addObserver:self selector:@selector(eventDidFire:) name:@"ILikeTurtlesEvent" object:nil]; - (void) eventDidFire:(NSNotification *)note { id obj = [note object]; NSLog(@"First one got %@", obj); }
ViewControllerB.m
NSNotificationCenter *note = [NSNotificationCenter defaultCenter]; [note addObserver:self selector:@selector(awesomeSauce:) name:@"ILikeTurtlesEvent" object:nil]; [note postNotificationName:@"ILikeTurtlesEvent" object:@"StackOverflow"]; - (void) awesomeSauce:(NSNotification *)note { id obj = [note object]; NSLog(@"Second one got %@", obj); }
It will be done (in any order, depending on which ViewController register is registered first):
First one got StackOverflow Second one got StackOverflow
Jed smith
source share