Reputation: 51
I use the following code in the viewDidLoad function of my RootViewController. In the AppDelegate the ApplicationDidBecomeActive function is called, but the RVC seems not to become the notification, because the function someMethod is not called. Anybody an idea what the problem is?
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(someMethod:)
name:UIApplicationDidBecomeActiveNotification object:nil];
....
-(void)someMethod:(NSNotification *)notification {
NSLog(@"OK");
}
Upvotes: 3
Views: 4170
Reputation: 29
if you want to add an observer for UIApplicationDidBecomeActiveNotification
that will also be triggered on the first launch, you should add the observer in the function
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
of your app delegate, like so:
[[NSNotificationCenter defaultCenter]
addObserver:self.window.rootViewController
selector:@selector(someMethod:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
(after setting the root view controller).
This works because didFinishWithLaunching is called before didBecomeActive in the appDelegate on the initial launch of the app.
Upvotes: 0
Reputation: 5290
Your problem is that in a single view controller app, viewDidLoad is not called until AFTER UIApplicationDidBecomeActiveNotification is posted. Therefore, you are registering for the notification after it occurs the first time. Subsequent activations should be caught, such as if you switch apps and come back, but you will miss the first one.
Upvotes: 8