1

Currently I'm only calling one method when application will enter foreground. How do I call various methods in @selector?

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(displayHappyFace)
                                             name:UIApplicationWillEnterForegroundNotification
                                           object:nil];
ConfusedDeer
  • 3,335
  • 8
  • 44
  • 72
  • 3
    call your other methods in displayHappyFace method – Mehul Mistri Jan 28 '14 at 04:35
  • That crossed my mind, but I can't because the second method is only needed when UIApplicationWillEnterForeground. I use displayHappyFace on viewDidLoad as well. – ConfusedDeer Jan 28 '14 at 04:44
  • possible duplicate of [Send and receive messages through NSNotificationCenter in Objective-C?](http://stackoverflow.com/questions/2191594/send-and-receive-messages-through-nsnotificationcenter-in-objective-c) – ConfusedDeer Jan 28 '14 at 05:22

3 Answers3

2

Just create a separate function for all your other function.

[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(AllFunction)
                                             name:UIApplicationWillEnterForegroundNotification
                                           object:nil];

All functions.

-(void) AllFunction
{
    [self displayHappyFace];
    [self otherFunction];
}
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
1

Add another observer to UIApplicationWillEnterForegroundNotification if you wish to keep the methods' logic separate:

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(displayHappyFace)
                                         name:UIApplicationWillEnterForegroundNotification
                                       object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(callOtherMethod)
                                         name:UIApplicationWillEnterForegroundNotification
                                       object:nil];

@selector supports only one method. Remember to remove self as the observer before releasing its memory, to avoid messages being passed to a nil object.

tipycalFlow
  • 7,594
  • 4
  • 34
  • 45
0

You can only put one selector there. Best practice is to create a new method called handleNotificationName: for each notification.

Example:

- (void)handleUIApplicationWillEnterForegroundNotification:(NSNotification *)aUIApplicationWillEnterForegroundNotification { }

This makes it really easy to figure out where your app handles each notification and makes code maintenance easy.

Inside the handler method you can call whatever methods you need to. You can have conditional logic also based on state you have or based the userInfo dictionary of the Notification ( if it has one ).

Don't forget to remove your notification observer in you object's dealloc method (at least, if not somewhere else because you might not want to always receive the notification depending on the use case)

uchuugaka
  • 12,679
  • 6
  • 37
  • 55