gpt4 book ai didi

ios - 将数据从子 Controller 传递回父 Controller

转载 作者:行者123 更新时间:2023-11-29 03:25:33 28 4
gpt4 key购买 nike

我正在开发一个 iPad 应用程序,其中需要处理 2 个 Controller 。我的父 Controller (比如 Controller A)有一个单元格,当单击该单元格时,它会通过 PushViewController 调用/导航到子 Controller (比如 Controller B),如下所示:

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

ControllerB *controllerB = [[ControllerB alloc] initWithStyle:UITableViewStyleGrouped];

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered
target:nil action:nil];
[self.navigationItem setBackBarButtonItem:backButton];
[self.navigationController pushViewController:controllerB animated:YES];
}

现在在 ControllerB 中,我处理一个开关,当用户从 ControllerB 到 ControllerA 然后返回到 ControllerB 来回导航时,该开关必须保持其状态,即用户更改 ControllerB 上的开关状态(例如:YES),然后返回到 ControllerA,然后当返回到 ControllerB 时,用户应该看到与导航回来时所打开的开关状态相同的状态。 (即:是)

我正在考虑将开关的状态从ControllerB发送回ControllerA,以便当ControllerB再次初始化时,可以发送开关的状态来设置开关的状态。

一些设计问题:

  • 在 Controller 之间建立交叉连接是个好主意吗?即 Controller A 有一个 Controller B 的对象,但为了发回开关状态,我计划在 Controller A 中创建一个属性来保存开关状态并在 Controller B 中将开关状态值分配给它。但为此我必须创建一个ControllerB 中的 ControllerA 对象,这可能是交叉连接。
  • 将数据从子 Controller 传递到父 Controller 的其他最佳方法是什么?

最佳答案

根本不需要让 Controller A 参与其中,只需不要每次推送时都实例化一个新的 Controller B 即可。为controllerB创建一个属性,并在推送之前检查 Controller 是否存在。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (! self.controllerB) {
self.controllerB = [[ControllerB alloc] initWithStyle:UITableViewStyleGrouped];
}

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered
target:nil action:nil];
self.navigationItem setBackBarButtonItem:backButton];
[self.navigationController pushViewController:self.controllerB animated:YES];
}

如果您出于任何原因需要将数据从 Controller B传递回 Controller A,那么您应该使用委托(delegate)来执行此操作(顺便说一句, Controller B不是 Controller A的子级。它们都是导航 Controller 的子级,这使他们成为同胞)。

关于ios - 将数据从子 Controller 传递回父 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20500236/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com