2012-02-16 77 views
1

我正在写一个简单的使用Xcode 4和iOS5的iPad应用程序。保持MasterViewController和DetailViewController同步

我正在使用UISplitViewController来管理主视图和详细视图。从主人到细节,一切都很好。我可以从列表中选择一个项目,并通过委托它更新详细视图。

我希望能够使用详细视图上的按钮删除项目。这在细节视图中很简单。但是,我似乎无法弄清楚如何更改主视图以反映项目已被删除的事实。

基本上委托模式似乎只有一种方式;从主人到细节,而不是从细节到主人。有没有办法将消息从细节传递给主人?

回答

1

你可以用NSNotifications来做到这一点。

#define ReloadMasterTableNotification @"ReloadMasterTableNotification" 

在你MasterViewController的viewDidLoad中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMasterTable:) name:ReloadMasterTableNotification object:_detailViewController]; 

在MasterViewController的dealloc的,如果你正在使用ARC:

[[NSNotificationCenter defaultCenter] removeObserver:self name:ReloadMasterTableNotification object:nil]; 

当你想使你的更新的detailViewController通知MasterViewController:

- (IBAction)onButtonPress { 
     NSIndexPath *path = [NSIndexPath indexPathForRow:indexToUpdate inSection:0]; 
     NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:path, @"IndexPath", nil]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:ReloadMasterTableNotification object:self userInfo:dict]; 
} 

- (void)reloadMasterTable:(NSNotification *)notification { 
    NSDictionary *dict = [notification userInfo]; 
    NSIndexPath *path = [dict objectForKey:@"IndexPath"]; 
    // update MasterViewController here 
} 

希望有帮助!

相关问题