0

i want to reload table when app comes to foreground from background, in my app delegate.m i did like this, but its not working

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    NSLog(@"applicationWillEnterForeground");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"EnteredForeground"
                                                        object:nil];

}

and in my viewController i am working like

- (void)viewDidLoad
{
    [super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(whenAppEnteredIntoForeground:) name:@"EnteredForeground" object:nil];
}
- (void)whenAppEnteredIntoForeground:(id)object {
    NSLog(@"log msg");
    [tblSearch reloadData];
}

what should i do? what mistake i am doing? any help please

1
  • you have tried, the reload table method when viewWillAppear ?
    – DogCoffee
    Sep 16, 2013 at 11:44

3 Answers 3

2

Firstly, you don't need to rebroadcast a notification when the app comes to the foreground, you can register for the notification from your view controller.

In your case, it's likely to be that the view is not loaded until after your secondary notification is sent, which is why your view controller cannot respond to it. Using breakpoints will confirm if this is the case.

Use this instead:

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

And you don't need to rebroadcast a notification from your appdelegate.

10
  • should i do in view will appear??
    – Noor
    Sep 16, 2013 at 11:48
  • when app comes in forgrond viewWillAppear calls first?
    – Noor
    Sep 16, 2013 at 11:50
  • viewWillAppear is called whenever the view is about to appear. Again. Stick a breakpoint on these methods and follow it through if you are unsure of the order of the calls.
    – Abizern
    Sep 16, 2013 at 11:52
  • first i wll try then i will accpt your ans
    – Noor
    Sep 16, 2013 at 11:52
  • What isn't working. The breakpoints? the data reloading, The notifications? Are you still using the notifications for the app coming to the foreground?
    – Abizern
    Sep 16, 2013 at 12:00
0
- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnteredIntoForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}


- (void)appEnteredIntoForeground:(id)object {

    [tableView reloadData];
}
-2

Make sure that your Reload Data must call from main Thread Otherwise it will not be reloaded.

1
  • There is nothing in the code that says it's being loaded off the UI thread.
    – Abizern
    Sep 16, 2013 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.