objective-c从不同的视图更新数据

时间:2011-06-15 14:10:50

标签: objective-c object uiviewcontroller navigation

我在我的应用中使用了基于导航的视图控制器。用户可以单击导航中的按钮以打开新视图,从中可以从几个不同的选项中进行选择。我想要发生的是当用户返回到原始视图时,应用程序将在第二个视图中选择的选项保存在原始视图中的对象中。

我尝试过一些东西,但都没有。

我尝试在原始视图控制器中创建一个对象,将其转换为属性:

@property (nonatomic, retain) NSString *testing;

然后在第二个视图控制器中,当用户选择一个选项时,用这样的东西更新它:

if (!oVC)
    oVC = [[OriginalViewController alloc] init];

oVC.testing = object;

我无法让它发挥作用。有人能指出我正确的方向吗?非常感谢。

1 个答案:

答案 0 :(得分:1)

使用NSUserDefaults

在一堂课中:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]
[prefs setInteger:1 forKey:@"integerKey"];

在另一堂课:

 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
 NSInteger myInt = [prefs integerForKey:@"integerKey"];

//Now myInt has the value 1.

在这里检查Documentation

您也可以使用NSNotification并传递NSDictionary类型:

NSDictionary *info = [NSDictionary dictionaryWithObject:@"Brad Pitt" forKey:@"User Name"];


[[NSNotificationCenter defaultCenter] postNotificationName:@"UserNameNotification" object:self userInfo:info];

然后你可以添加观察者并为相应的选择器编写方法。