Tuesday, August 2, 2011

How to post NSNotification in iPhone ?

If you have an event happening in one view and its response/result  of the event is supposed to happen in another view. Then NSNotification are the best way to manage it. They are basically used for passing information between 2 views.

After event E has happened in View A, you want to send message to View B to update itself.

In this example, font was updated from a table list in View A and View B is being informed about it.

So you write notification listener in View B as following:


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(FontUpdated:) name:@"FontUpdated" object:nil];

You can write it anywhere .. like in viewDidLoad ...

Then notification  is posted by View A after user has selected the font and confirmed it (say 
-(void)confirmed is executed). You add following piece in your code in  -(void)confirmed method.

[[NSNotificationCenter defaultCenter] postNotificationName:@"FontUpdated" object:fontString];

No comments:

Post a Comment