Ios5 保持MasterViewController和DetailViewController同步

Ios5 保持MasterViewController和DetailViewController同步,ios5,uisplitviewcontroller,Ios5,Uisplitviewcontroller,我正在使用Xcode 4和iOS5编写一个简单的iPad应用程序 我正在使用UISplitViewController来管理主视图和详细视图。从大师到细节,一切都很顺利。我可以从列表中选择一个项目,并通过代理更新详细视图 我希望能够使用“详细信息”视图上的按钮删除项目。在详图视图上执行此操作非常简单。但是,我似乎不知道如何更改主视图以反映项目已被删除的事实 基本上,委托模式似乎只走一条路;从大师到细节,而不是从细节到大师。有没有办法将消息从详细信息传递到主信息?您可以使用NSNotificati

我正在使用Xcode 4和iOS5编写一个简单的iPad应用程序

我正在使用UISplitViewController来管理主视图和详细视图。从大师到细节,一切都很顺利。我可以从列表中选择一个项目,并通过代理更新详细视图

我希望能够使用“详细信息”视图上的按钮删除项目。在详图视图上执行此操作非常简单。但是,我似乎不知道如何更改主视图以反映项目已被删除的事实


基本上,委托模式似乎只走一条路;从大师到细节,而不是从细节到大师。有没有办法将消息从详细信息传递到主信息?

您可以使用NSNotifications来完成

#define ReloadMasterTableNotification @"ReloadMasterTableNotification"
在您的viewDidLoad MasterViewController中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMasterTable:) name:ReloadMasterTableNotification object:_detailViewController];
- (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
}
如果使用ARC,则在MasterViewController的解除锁定中:

[[NSNotificationCenter defaultCenter] removeObserver:self name:ReloadMasterTableNotification object:nil];
当您要在detailViewController中进行更新以通知MasterViewController时:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMasterTable:) name:ReloadMasterTableNotification object:_detailViewController];
- (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
}

希望有帮助

您可以使用NSNotifications来完成

#define ReloadMasterTableNotification @"ReloadMasterTableNotification"
在您的viewDidLoad MasterViewController中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMasterTable:) name:ReloadMasterTableNotification object:_detailViewController];
- (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
}
如果使用ARC,则在MasterViewController的解除锁定中:

[[NSNotificationCenter defaultCenter] removeObserver:self name:ReloadMasterTableNotification object:nil];
当您要在detailViewController中进行更新以通知MasterViewController时:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadMasterTable:) name:ReloadMasterTableNotification object:_detailViewController];
- (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
}
希望有帮助