I saw a few examples about adding observer and handle in the same class, but what I want to know is if it's possible to add observer in first view controller and handle it in second view controller?
I want constantly send distance from first view controller and handle it in the 2nd one.
The 2nd view controller added as a sub view: addSubview
, addChildViewController
.
It's something like broadcast in android.
Yes it is possible. NSNotificationCenter
works exactly in that way.
Firstly, you will have to register the listener in the first view controller as below.
-(void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(somethingHappens:) name:@"notificationName" object:nil];
}
-(void)somethingHappens:(NSNotification*)notification
{
}
Secondly, post the notification from the second view controller as below.
[[NSNotificationCenter defaultCenter] postNotificationName:@"notificationName" object:obj];
The system will broadcast the notification to all the listeners.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With