保持MasterViewController和DetailViewController同步 [英] Keeping MasterViewController and DetailViewController in sync
问题描述
我正在使用Xcode 4和iOS5编写一个简单的iPad应用程序。
I am writing a simple iPad appication using Xcode 4 and iOS5.
我正在使用UISplitViewController来管理主视图和详细视图。从主人到细节,一切都很好。我可以从列表中选择一个项目,并通过委托更新详细信息视图。
I am using a UISplitViewController to manage a master and detail view. Everything is working fine going from master to detail. I can select an item from the list and through the delegate it updates the detail view.
我希望能够使用详细信息视图上的按钮删除项目。这在细节视图上非常简单。但是,我似乎无法弄清楚如何更改主视图以反映项目已被删除的事实。
I want to be able to delete an item using a button on the detail view. This is very simple to do on the detail view. However, I cannot seem to figure out how to change the master view to reflect the fact that an item has been deleted.
基本上委托模式似乎只有一种方式;从主人到细节而不是从细节到主人。有没有办法将消息从详细信息传递给主服务器?
Basically the delegate pattern seems to only go one way; from master to detail and not from detail to master. Is there a way to pass messages from the detail to the master?
推荐答案
您可以使用NSNotifications进行此操作。
You can do it with NSNotifications.
#define ReloadMasterTableNotification @"ReloadMasterTableNotification"
在MasterViewController的viewDidLoad中:
In your viewDidLoad of MasterViewController:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMasterTable:) name:ReloadMasterTableNotification object:_detailViewController];
如果你正在使用ARC,那么在MasterViewController的dealloc中:
in dealloc of MasterViewController if you're using ARC:
[[NSNotificationCenter defaultCenter] removeObserver:self name:ReloadMasterTableNotification object:nil];
当你想在detailViewController中进行更新以通知MasterViewController时:
When you want to make your update in the detailViewController to notify the 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
}
希望有所帮助!
这篇关于保持MasterViewController和DetailViewController同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!