How do I call a method when my application comes back from the background?
I know there are methods that can be called in the app delegate, but I want to call a method in my view.
What is the best way of doing this?
Thanks in advance!
How do I call a method when my application comes back from the background?
I know there are methods that can be called in the app delegate, but I want to call a method in my view.
What is the best way of doing this?
Thanks in advance!
You should have your desired view controller register for UIApplicationWillEnterForegroundNotification
notification, such as in initWithNibName:nibBundleOrNil:
- (id)initWithNibName(NSString *)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yourMethod:) name:UIApplicationWillEnterForegroundNotification object:nil];
// Whatever else your init method should do here...
}
return self;
}
- (void)yourMethod:(NSNotification *)notification
{
// whatever you want to do here...
}
Make sure you also unregister in dealloc
:
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}