设置视图iOS应用程序,其变量可以在其他视图控制器中使用

时间:2013-10-27 09:48:11

标签: ios

我是iOS编程新手,正在创建一个包含多个viewControllers的应用。其中一个viewControllers是一个设置选项卡,我想存储一些变量,以便我可以跨控制器使用它。有这样做的标准方法吗?我目前正在研究NSUserDefaults,但任何设计模式都会受到赞赏。

2 个答案:

答案 0 :(得分:0)

<强> 1。使用故事板 -

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    if ([segue.identifier isEqualToString:@"xyzView"]) 
    {
        MyViewController *destViewController = segue.destinationViewController;
        destViewController.objectPassedFromPrevViewController = dataToBePassedToNextViewController
    }
}

<强> 2。使用导航控制器 -

DestinationViewController *destinationController = [[DestinationViewController alloc] init];
destinationController.objectPassedFromPrevViewController = dataToBePassedToNextViewController;
[self.navigationController pushViewController:destinationController animated:YES];

第3。添加子视图时 -

DestinationViewController *destinationController = [[DestinationViewController alloc] init];
destinationController.objectPassedFromPrevViewController = dataToBePassedToNextViewController;
[self.view addSubview:destinationController.view];

答案 1 :(得分:0)

您使用NSUserDefaults的倾向是好的。它用于存储和检索应用程序的用户设置和首选项。