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

122 views Asked by At

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?

1

There are 1 answers

3
ChintaN -Maddy- Ramani On BEST ANSWER

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.