How to update a view after entering applicationWillEnterForeground?

Possible duplicate:
How to know when the controller resumed from the background?

How to update a view after user login in applicationWillEnterForeground?

I want to end a call, for example, HomeViewController.

I have an update function in the HomeViewController, and I want it when the user comes in to call the update function and reload the table data.

+6
source share
3 answers

Any class can register at UIApplicationWillEnterForegroundNotification and respond accordingly. It is not reserved for the application delegate and helps to better separate the source code.

+9
source

Create a viewDidLoad method similar to this for HomeViewController

 - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yourUpdateMethodGoesHere:) name:UIApplicationWillEnterForegroundNotification object:nil]; } // Don't forget to remove the observer in your dealloc method. // Otherwise it will stay retained by the [NSNotificationCenter defaultCenter]... - (void) dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; [super dealloc]; } 

If your ViewController is a tableViewController, you can also directly call the reload data function:

 - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] addObserver:[self tableView] selector:@selector(reloadData) name:UIApplicationWillEnterForegroundNotification object:nil]; } - (void) dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; [super dealloc]; } 

Or you can use the block:

 [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) { [[self tableView] reloadData]; }]; 
+7
source

You can declare a property in the delegation class of the application that points to the HomeViewController object. Then you can call your update function in applicationWillEnterForeground.

0
source

All Articles