简体   繁体   中英

Save UITableView property when applicationDidEnterBackground and load back it applicationDidBecomeActive in iOS

In my iOS application, I'm having a UITableView inside my UIViewController. After data loading completed to the UITableView, when I press the Home button of the iPhone, the application will enter to the background. It will execute following method.

- (void)applicationDidEnterBackground:(UIApplication *)application {

When I tap on the application icon and launch the app, it will call following methods in AppDelegate

- (void)applicationWillEnterForeground:(UIApplication *)application {

and

- (void)applicationDidBecomeActive:(UIApplication *)application {

but none UIViewController methods. Therefore what I did was, called a custom method created by myself which is located inside my UIViewController class like below. This code go inside applicationDidBecomeActive method.

MyViewController *tViewCont = [MyViewController alloc];
[tViewCont remindToPopulate];

I put a log message and confirmed that remindToPopulate method is executing. Inside that method I want to reload the UITableView.

But at this time the UITableView property that I've declared is set to nil. What is the proper way of saving that UITableView property and load it back with the latest data?

For that you can add notification for UIApplicationDidBecomeActiveNotification or UIApplicationWillEnterForegroundNotification in viewDidLoad in controller.

just add

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(yourmethod) name:UIApplicationDidBecomeActiveNotification object:nil];

and in yourmethod

-(void)yourmethod{
 [yourtableview reloadData];
}

you can also add UIApplicationWillEnterForegroundNotification notification. add as per your need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM