来自嵌套导航堆栈的委派

我有标准的视图控制器作为模态视图控制器的代表。 该模式视图控制器包含在导航控制器中。

在呈现模式之后,将另一个视图控制器推到导航堆栈上,我想将一些数据传回给最初的委托视图控制器(呈现模式)。

我是否应该将导航堆栈中的消息先传递给模态导航控制器的根视图控制器,然后使用该控制器的委托方法?

要么

我应该只是将委托属性一起传递给嵌套的视图控制器,然后直接调用委托,实现一个单独的协议。 这样做的工作,但我必须使用

@property (nonatomic, weak) id delegate; 

代替

 @property (nonatomic, weak) id <NestedViewDelegate> delegate; 

否则,当我从堆栈中的预览视图控制器传递委托时,会出现不兼容的types错误:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

{

 NestedViewController *nest = [[NestedViewController alloc] init]; // @property id <RootViewControllerDelegate> delegate [nest setDelegate:[self delegate]]; [[self navigationController] pushViewController:nest animated:YES]; 

}

这种情况下的最佳做法是什么?

谢谢

我会考虑使用通知来解耦这个。 结帐NSNoticiationCenter。

你在你的根注册通知并发布给你的孩子。

http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/nsnotificationcenter_Class/Reference/Reference.html

在你的Child对象中,你可以这样做:

 [[NSNotificationCenter defaultCenter] postNotificationName:kMyNotificationName object:self userInfo:@{ @"key" : @"value" }]]; 

kMyNotificationName在共享位置定义,如pch或Constants.h。

在Root中,你可以做这样的事情,无论是在init还是在你推动孩子的时候。

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myNotificationName:) name:kMyNotificationName object:nil]; 

不要忘记在你的dealloc中或当你popup孩子时删除观察者。

 [[NSNotificationCenter defaultCenter] removeObserver:self kMyNotificationName object:nil]; 

现在你可以像这样处理通知了:

 - (void)myNotificationName:(NSNotification *)note { NSDictionary *userInfo = [note userInfo]; // Do stuff using the information passed in. }