“代表"的替代方案是什么?在控制器之间传递数据?

2023-09-12移动开发问题
0

本文介绍了“代表"的替代方案是什么?在控制器之间传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

是否有替代委托"的方法将数据从一个控制器传回另一个控制器?

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.

推荐答案

你可以使用很多方法:

  1. 调用超级控制器的方法,可能需要强制转换
  2. 通知
  3. 简单的Key-Value-Observing
  4. 核心数据

1 的示例.

你的 MainViewController 的接口:为要传递的数据添加一个公共方法

- (void)newDataArrivedWithString:(NSString *)aString;

MainViewController 显示 ChildController

- (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;

子控制器向 MainViewController 传递数据

- (void)passDataToMainViewController
{
    NSString * someDataToPass = @"foo!";
    [self.mainViewController newDataArrivedWithString:someDataToPass];
}

这篇关于“代表"的替代方案是什么?在控制器之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

Cocos2d - 如何检查不同层中对象之间的交集
Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)...
2024-08-12 移动开发问题
8

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

突出显示朗读文本(在 iPhone 的故事书类型应用程序中)
Highlight Read-Along Text (in a storybook type app for iPhone)(突出显示朗读文本(在 iPhone 的故事书类型应用程序中))...
2024-08-12 移动开发问题
9

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

如何将 32 位 PNG 转换为 RGB565?
How to convert 32 bit PNG to RGB565?(如何将 32 位 PNG 转换为 RGB565?)...
2024-08-12 移动开发问题
21