【发布时间】:2013-10-21 09:44:37
【问题描述】:
我正在开发一个应用程序,我需要在视图控制器之间传递数据。我知道这是一个常见问题,但我找不到我的问题的答案:我能够将数据从 FirstViewController(在我的情况下为 MasterViewController)传递到 SecondViewController(SettingsViewController)但不是逆转。发生的情况是我从 SecondViewController.m 文件中的 FirstViewController 调用了一个方法。这有效,它记录数据。但是当我退出 SecondViewController(使用[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];)时,数据会被重置。
我尝试使用其他方法来传递数据,但没有奏效。我正在使用此代码来传递数据:
MasterViewController *vc = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil];
[vc setPorts:SelectedPorts];
我也尝试将[vc setPorts:SelectedPorts]; 替换为vc.selectedCellIndexes = SelectedPorts;,但出现了同样的问题。
setPorts 方法在 FirstViewController.h 文件中声明,SelectedPorts 是我在 SecondViewController.m 中声明的变量(我检查过它不是 nil)。
这是 FirstViewController.m 中的setPorts::
- (void) setPorts:(id)selectedPorts {
selectedCellIndexes = selectedPorts;
NSLog(@"selectedCellIndexes : %@", selectedCellIndexes);
}
这记录了良好的值,但是当我将其记录到 FirstViewController.m 中的 viewWillAppear 时,它会重置为我从 SecondViewController.m 调用该方法之前的值。
澄清一下,如果我不退出 SecondViewController.m,数据不会重置。
我确实阅读了您所有的 cmets,非常感谢您的帮助。为方便起见,我使用了一个全局变量。
感谢您的帮助。
【问题讨论】:
-
数据被重置为 secondViewController 并且它的 iVar 被释放。您需要在主视图控制器上创建一个 ivar,以一致地保存您正在谈论的数据。
-
你能给我一些建议吗?
-
@jrock007 您如何向我们展示您是如何尝试声明和实现委托协议的?
-
怎么了@jrock007? 7人花时间写答案。可以让我们更新吗?
标签: iphone ios objective-c uiviewcontroller ios7