iphone - 在 View Controller ios 7 之间传递数据

标签 iphone ios objective-c uiviewcontroller ios7

我正在开发一个应用程序,我需要在 View Controller 之间传递数据。我知道这是一个常见问题,但我找不到问题的答案:我能够将数据从 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.selectedCellIndexes = SelectedPorts; 替换 [vc setPorts:SelectedPorts]; 但同样的问题出现了。

setPorts 方法在 FirstViewController.h 文件中声明,SelectedPorts 是我在 SecondViewController.m 中声明的变量(我检查过它不是 nil)。

这是 FirstViewController.m 中的 setPorts::

- (void) setPorts:(id)selectedPorts {
selectedCellIndexes = selectedPorts;
NSLog(@"selectedCellIndexes : %@", selectedCellIndexes);
}

这记录了良好的值(value),但是当我在 FirstViewController.m 的 viewWillAppear 中记录它时,它被重置为我从 SecondViewController.m 调用方法之前的值。

澄清一下,如果我不退出 SecondViewController.m,数据不会重置。

我确实阅读了您所有的评论,非常感谢您的帮助。为了方便起见,我使用了一个全局变量。

感谢您的帮助。

最佳答案

  • 您在 MasterViewController 中有一个端口列表,您希望在 SettingsViewController 中使用它。
  • MasterViewController 可以保存此列表,SettingsViewController 应该可以访问它。

SettingsViewController 中,有一个 setSelectedPort 方法:

@property (nonatomic, retain) id selectedPorts

- (void) setPorts:(id)selectedPorts;

该方法将选定的端口列表保存到一个属性中。

MasterViewController 中,调用 SettingsViewController 并为其提供列表。

SettingsViewController *vc = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil];
[vc setSelectedPorts:yourValue]; 

当在 SettingsViewController 中修改列表时,即使您离开 SettingsViewControllerMasterViewController 中包含的端口列表也不会移动.

关于iphone - 在 View Controller ios 7 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19491088/

相关文章:

ios - 编码和解码自定义对象

iphone - 横向和纵向 View 中的旋转问题

objective-c - 在 Cocoa/OSX 中覆盖鼠标图像

ios - 隐藏音量 HUD(避免使用加载 View )

iphone - ASIHTTPRequest - 设置忽略缓存/代理

ios - 使用闭包?

ios - 为什么显示 Google 插页式广告会改变我的导航栏的高度? Xcode 11.3、iOS 13

ios - 在 Core Data 中更改 NSOrderedSet 顺序的尝试和磨难

iphone - 延迟 UITableView 加载

iphone - NSmutablearray 上的 uitextfield 输入