从不同角度客观地更新数据 [英] objective-c updating data from a different view

查看:64
本文介绍了从不同角度客观地更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using a navigation based view controller in my app. Users can click on a button in the navigation to open a new view where they can select from a few different options. What I'd like to happen is when the user returns to the original view, the app saves the option chosen in the second view in an object in the original view.

我已经尝试了一些方法,但是没有一个起作用.

I've tried a few things but none of them work.

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

I've tried creating an object in the original view controller, turning it into a property:

@property (nonatomic, retain) NSString *testing;

然后在第二个视图控制器中,当用户选择一个选项时,用如下所示更新它:

then in the second view controller, updating it with something like this when the user selects an option:

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

oVC.testing = object;

我无法正常工作.有人可以指出我正确的方向吗?将不胜感激.

I can't get it to work. Could someone point me in the right direction? It would be greatly appreciated.

推荐答案

使用NSUserDefaults

Use 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.

检查文档在这里!

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

You could also have NSNotification and pass an NSDictionary type:

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


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

然后您可以添加观察者并为相应的Selector编写方法.

And then you could add observers and write the method for the corresponding Selector.

这篇关于从不同角度客观地更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆