2011-01-20 67 views
7

我有一個非常簡單的問題,但我仍然找到我的方式圍繞可可。 我有一個正常的rootViewController應用程序在Xcode中創建。在AppDelegate中,我有一個函數來更新數據庫。當運行時發送推送消息(didReceiveRemoteNotification :)時,數據被更新。從AppDelegate重新加載tableView

但是,我如何獲得RootViewController的句柄,告訴它更新其對象,然後重新加載表(這是一個函數)?

回答

20

您可以使用NSNotificationCenter,看到NSNotificationCenter Class Reference

在你rootViewControllerviewDidLoad,添加以下內容:

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

,並添加下面的方法:

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

在你AppDelegate「 s didReceiveRemoteNotification,請添加以下內容:

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

那麼值得一問,因爲它比我嘗試過的任何東西都簡單得多,而且對於更多功能非常有用!太感謝了! – 2011-01-20 20:40:20