Reload a tableView from the AppDelegate

2019-04-25 08:46发布

问题:

I have a pretty simple question, but am still finding my way around cocoa. I have a normal rootViewController App as created in Xcode. In the AppDelegate I have a function to update the database. When a Push-message comes in while running (didReceiveRemoteNotification:) the data is updated.

But how do I get a handle on the the RootViewController telling it to update its objects and then reload the table (which is a function)?

回答1:

You can use NSNotificationCenter, see NSNotificationCenter Class Reference

In your rootViewController's viewDidLoad, add the following:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView:) name:@"updateRoot" object:nil];

and add the following method:

- (void)updateView:(NSNotification *)notification {
    [myTableView reloadData]; 
}

In your AppDelegate's didReceiveRemoteNotification, add the following:

[[NSNotificationCenter defaultCenter] postNotificationName:@"updateRoot" object:nil];