对于此类问题可以使用委托
代码来自RayWenderlichs 教程 http://www.raywenderlich.com/5191/beginning-storyboards-in-ios-5-part-2:
.h 在你的 childViewController 中:
@class ChildViewController;
@protocol ChildViewControllerDelegate <NSObject>
- (void)childViewControllerDidSelect:(id)yourData;
@end
@interface childViewController : UITableViewController
@property (nonatomic, weak) id <ChildViewControllerDelegate> delegate;
- (IBAction)cancel:(id)sender;
- (IBAction)done:(id)sender;
@end
childViewController 中的 .m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.delegate childViewControllerDidSelect:myObject];
[self.navigationController popViewControllerAnimated:YES];
}
在你的parentViewController.h中采用协议
@interface ParentViewController : UITableViewController <ChildViewControllerDelegate>
并实现委托方法
- (void)childViewControllerDidSelect:(id)yourData
{
self.someProperty = yourData
}
并且不要忘记在推送之前设置委托:
...
ChildViewController *vc = [ChildViewController alloc] init];
vc.delegate = self;
[self.navigationController pushViewController:vc animated:YES];
这是一些关于委托模式的文档:代表和数据来源 https://developer.apple.com/library/ios/#documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html