在两个视图控制器之间传递数据似乎已经使用委托来解决。我的情况有点不同,既然我是新人,我不知道我能否与代表解决。iOS:将数据从ChildViewController传递给GrandParentViewController?
我有3个视图控制器。 GrandParent
,Parent
和Child
。
GrandParent
实例化Parent
显示列表CategoryGroups
。
点击CategoryGroup
实例化Child
视图控制器显示Categories
的列表。
我希望当用户点击任何Category
时,GrandParent
会得知被点击的Category
。
我现在拥有什么?
在Child.h
视图控制器
@protocol CategorySelectDelegate<NSObject>
- (void) categorySelected:(CategoryModel *) categoryModel;
@end
在Child.m
视图控制器
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"selected category:%@", _categories[(NSUInteger) indexPath.row]);
[self.delegate categorySelected:_categories[(NSUInteger) indexPath.row]];
[self dismissViewControllerAnimated:YES completion:nil];
}
在GrandParent.h
@interface GrandParent : UIViewController<CategorySelectDelegate>
在GrandParent.m
- (void)viewDidLoad {
[super viewDidLoad];
ChildViewController *categoryViewController = [[ChildViewController alloc] init];
childViewController.delegate = self;
}
- (void)categorySelected:(CategoryModel *)categoryModel {
_categoryLabel.text = categoryModel.name;
NSLog(@"categoryLabel:%@", _categoryLabel.text);
}
但我知道这是不正确,因为GrandParent
是不的一个直接实例Child
,它始终是父母谁生下Child
问题
- 如何通过categoryModel
从Child
到GrandParent
?
- 一般来说,我如何将数据从一个子控制器传递回任何祖先控制器?
UPDATE
现在,我已经添加了2名代表来解决这个问题
一)从Child
到Parent
b 1名代表。)1名代表从Parent
到GrandParent
这工作,但我不认为这是一个好的设计,数据需要在两个或更多的视图控制器之间传递,因为最终会创建新的代理来传递值。
那么,我使用委托在vcs之间传递这种数据。但我也使用CoreData或NSUser在vcs之间共享一些其他数据。 – leonard 2014-11-24 03:44:37