0

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?

AnujAroshA
  • 4,623
  • 8
  • 56
  • 99
  • 1
    add `[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(yourmethod) name:UIApplicationDidBecomeActiveNotification object:nil];` in `viewDidLoad`. it will fire when your appdidbecomeactive.you can also add `UIApplicationWillEnterForegroundNotification` . add as per your need – ChintaN -Maddy- Ramani Nov 11 '14 at 13:20
  • did you checked with topviewcontroller insted of alloc? – Smile Nov 11 '14 at 13:25
  • @Chinttu-Maddy-Ramani thanks. Seems your answer is correct. – AnujAroshA Nov 11 '14 at 13:49
  • @ismail You mean `UIViewController* root = _window.rootViewController;` code? Please explain what you are saying. Then I can check. – AnujAroshA Nov 11 '14 at 13:50
  • if you are using navigation controller then i mean like this MyViewController *tViewCont=self.navigationcontroller.rootviewcontroller; – Smile Nov 11 '14 at 13:55

1 Answers1

0

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.

ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48