什么是“代表”的替代方案在控制器之间传递数据? [英] What are alternatives to "delegates" for passing data between controllers?
本文介绍了什么是“代表”的替代方案在控制器之间传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有替代代表将数据从一个控制器传回到另一个控制器?
Are there alternatives to "delegates" to pass back data from one controller to another?
似乎很多工作实现代理只是为了传回来自一个子控制器,返回到父控制器。有没有其他方法?是块一个答案,如果是这样的代码将是很棒的。
Just seems like a lot of work implementing a delegate just to pass back the result from a child controller, back to the parent controller. Is there not another method? Are "blocks" one answer, and if so some example code would be great.
推荐答案
你可以使用很多方法: p>
You could use many ways:
- Calling a method of the super controller, needs casting maybe
- Notifications
- Simple Key-Value-Observing
- Core Data
示例为1。
界面:为要传递的数据添加公共方法
- (void)newDataArrivedWithString:(NSString *)aString;
显示ChildController的MainViewController
- (void)showChildController
{
ChildController *childController = [[ChildController alloc] init];
childController.mainViewController = self;
[self presentModalViewController:childController animated:YES];
[childController release];
}
子控制器头/接口:为mainViewController添加一个属性
@class MainViewController;
@interface ChildController : UIViewController {
MainViewController *mainViewController;
}
@property (nonatomic, retain) MainViewController *mainViewController;
Child Controller将数据传递给MainViewController
- (void)passDataToMainViewController
{
NSString * someDataToPass = @"foo!";
[self.mainViewController newDataArrivedWithString:someDataToPass];
}
这篇关于什么是“代表”的替代方案在控制器之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文