如何实现NSUserDefault以通过多个视图进行访问 [英] How to implement NSUserDefault to access through multiple views

查看:148
本文介绍了如何实现NSUserDefault以通过多个视图进行访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在多个视图中共享字符串和整数,以便在介绍性的iOS开发课程中进行最终项目,大约50,并且我想知道我将如何去做...我知道MVC范例是最好的但是我们没有得到非常先进的东西,特别是核心数据。我正在考虑使用NSUserDefaults,我知道这是错的,但这个应用程序不一定非常快,我认为这对我来说是最简单的方法。我的问题是,我将在何处以及如何声明我将使用的NSUserDefault对象?我们在课堂上唯一一次使用它是在一个视图中。如果我在加载的第一个视图中声明它我知道我可以使用其他视图访问它但是我是否需要将第一个视图的头文件导入其他视图中,或者它是否可以访问?

I'm trying to share strings and integers throughout multiple views for a final project in an introductory iOS development course, around 50, and am wondering how I would go about doing this... I know the MVC paradigm is best but we didn't get to very advanced stuff, especially core data. I was thinking of using NSUserDefaults, I know it's wrong but this app doesn't have to be blazingly fast, and I think this would be the simplest way for me. My question is, where and how would I declare the NSUserDefault object I would be using? The only time in class that we used it was in one view. If I declare it in the first view that's loaded I know I can access it with the other views but do I need to import the header file of the first view into each of the others or will it be accessible regardless?

所以我想我会尝试通过在所有 ViewControllers 中使 NSUserDefaults 属性来简化操作。然后,为了尝试获取值,我将每个 ViewControllers 中的头文件实现到需要访问NSUserDefaults中存储的数据的视图中(该文件还有一个<$ c) $ c> NSUserDefault property)。该应用程序运行非常严重,直到我到达该屏幕,没有任何更新。我确定我的错误在于我如何实现 NSUserDefaults ,但我不确定如何正确地执行它。当我们在课堂上浏览它们时(实际上它是一个定向研究,Sam在24小时内教你自己的iPhone应用程序开发(呕吐,这让我感到愤怒)),我们唯一一次使用它们是在单个视图应用程序中改变了白色背景的阿尔法,因此它可以用作闪光灯。这就是它的呈现方式:

So I thought I would try to make things easier by making NSUserDefaults a property in all of the ViewControllers. Then to attempt to get the values back I implemented the headers from each of the ViewControllers into the view that needs to access the data stored in NSUserDefaults (which also has an NSUserDefault property). The app runs terrifically until I get to that screen and nothing is updated. I'm sure my mistake lies somewhere in how I implemented the NSUserDefaults but I am unsure of how to do it correctly. When we went over them in class (actually it's a Directed Study with "Sam's Teach Yourself iPhone Application Development in 24 Hrs" (puke, it makes me rage)), the only time we used them was in a single view application that changed the alpha of a white background so it could be used as a flash light. This is how it was presented:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger: var forKey: kVar];
[userDefaults synchronize];

这是我在我的应用中尝试的内容:

This is what I was attempting in my app:

...

@interface AppetizerViewController : UIViewController{
    NSUserDefaults *userDefaults;

    ...
}

@property(assign)NSUserDefaults *userDefaults;



然后,当我点击一个按钮时,我想将该值保存到 userDefaults



Then when I clicked a button I wanted to save that value to userDefaults:

-(IBAction)plusEgg:(id)sender{
    eggs++;
    eggString = [NSString stringWithFormat:@"%d",eggs];
    eggQty.text = eggString;
    eggOrderedQty.text = eggString;

    [userDefaults setInteger:eggs forKey:kEggQty];
    [userDefaults synchronize];
    [self updateAppSubtotal];
}

-(IBAction)minusEgg:(id)sender{
    eggs--;

    if(eggs < 0){
        eggs = 0;
    }

    eggString = [NSString stringWithFormat:@"%d",eggs];
    eggQty.text = eggString;
    eggOrderedQty.text = eggString;

    [userDefaults setInteger:eggs forKey:kEggQty];
    [userDefaults synchronize];
    [self updateAppSubtotal];
} 



然后,因为我有 NSUserDefault Keys 在所有视图的头文件中,我将所有头文件实现到 BillViewController 之类,并尝试访问它们这个:



Then, since I had all of the constants for the NSUserDefault Keys in the header files of all of the views, I implemented all of the header files into the BillViewController like this and tried accessing them like this:

-(void)populateLabels{
    pieFinalQty.text = [NSString stringWithFormat:@"%d",
        [userDefaults integerForKey:kPieQty]];
    hazFinalQty.text = [NSString stringWithFormat:@"%d",
        [userDefaults integerForKey:kHazQty]];
    briFinalQty.text = [NSString stringWithFormat:@"%d",
        [userDefaults integerForKey:kBriQty]];
choFinalQty.text = [NSString stringWithFormat:@"%d",
        [userDefaults integerForKey:kChoQty]];
iceFinalQty.text = [NSString stringWithFormat:@"%d",
        [userDefaults integerForKey:kIceQty]];
}


-(void)getSubtotal{
    NSString *preSubString;
    NSString *subString;

    subFloat = [userDefaults floatForKey:kAppSub]+[userDefaults floatForKey:kBevSub]+
        [userDefaults floatForKey:kDesSub]+
        [userDefaults floatForKey:kEntSub]+[userDefaults floatForKey:kSidSub];

    if(subFloat < 10){
        preSubString = [[NSString alloc]initWithFormat:@"%1.2f",subFloat];
    }else if(subFloat < 100 && subFloat >= 10){
        preSubString = [[NSString alloc]initWithFormat:@"%2.2f",subFloat];
    }else if(subFloat < 1000 && subFloat >= 100){
        preSubString = [[NSString alloc]initWithFormat:@"%3.2f",subFloat];
    }else{
        preSubString = [[NSString alloc]initWithFormat:@"%4.2f",subFloat];
    }
    subString = [[NSString alloc]initWithFormat:@"$%@",preSubString];
    billSubtotal.text = subString;

    [preSubString release];
    [subString release];
} 

我很抱歉你们试图向我解释的是完全结束了显然我的头脑和我的命名惯例有点不同,但我已经在这里工作了大约15个小时,几次休息,这是在明尼苏达州凌晨4点左右。我怎么能这样做?我很抱歉它可能需要明确如何实现它。

I'm sorry that what you guys tried to explain to me was completely over my head apparently and that my naming conventions are kinda f***ed, but I've been working on this for about 15 hours straight with few breaks and it's about 4:00am here in MN. How can I go about this? I'm sorry too that it might have to be kind of explicit on how to implement it.

推荐答案

关于<的好处code> NSUserDefaults 您无需在任何地方实例化它:它已经可以从您的应用程序中的任何位置访问。只需调用 [NSUserDefaults standardUserDefaults] ,然后读取或写入该对象。在对象上调用 synchronize 以确保更改存储在磁盘上,然后在下一个位置检索 standardUserDefaults 对象将有更新的信息。

The good thing about NSUserDefaults is you don't need to instantiate it anywhere: it's already reachable from anywhere in your application. Just call [NSUserDefaults standardUserDefaults], and read or write to/from that object. Call synchronize on the object to make sure changes are stored on disk and then the next place you retrieve the standardUserDefaults object it will have the updated information.

编辑:根据您的上述代码,您可以进行以下更改以使用标准用户默认值对象:

Based on your above code, you could make the following changes to utilise the standard user defaults object:


  1. 摆脱 @property(assign)NSUserDefaults userDefaults; 和你的成员变量 NSUserDefaults userDefaults;

  2. 想要读取或写入用户默认值的所有地方,创建一个临时变量:
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];

  1. Get rid of your @property (assign) NSUserDefaults userDefaults; and your member variable NSUserDefaults userDefaults;
  2. Everywhere you want to read or write things to the user defaults, create a temporary variable: NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

另一种方式这样做,这似乎是你想要做的,将 @property 设置为标准的用户默认对象,所以它不一定是通过调用 standardUserDefaults 检索(因为它好像你使用它很多,这可能是你应该去的方式)。

The other way you could do it, which seems to be what you were trying to do, would be to set up the @property to be the standard user defaults object so it doesn't have to be retrieved by calling standardUserDefaults (since it seems you use it quite a bit, this is probably the way you should go).

这样做:


  1. 保留 @property ,但将其设置为(保留) (assign) standardUserDefaults 对象 autorelease d ,所以你需要自己坚持下去)。 (可选)您可以删除 NSUserDefaults userDefaults; 行,因为 @property 无论如何都会为您创建

  2. 初始化视图控制器时(或者在 viewDidLoad 中,就像我在这里做的那样),将 userDefaults 变量设置为标准用户默认对象:

  1. Leave your @property, but set it to (retain) instead of (assign) (the standardUserDefaults object is autoreleased, so you need to hold on to it yourself). (optional) You can remove the NSUserDefaults userDefaults; line, since the @property creates this for you anyway
  2. When you initialise your view controller (or in viewDidLoad, like I'm doing here), set the userDefaults variable to the standard user defaults object:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.userDefaults = [NSUserDefaults standardUserDefaults];
}


  • 在与初始化方法对应的方法中释放用户默认值变量( dealloc 用于 init 方法,或 viewDidUnload 用于 viewDidLoad ):

  • Release the user defaults variable in the method corresponding to your initialisation method (dealloc for an init method, or viewDidUnload for viewDidLoad):

    - (void)viewDidUnload
    {
        self.userDefaults = nil;
    }
    


  • 这篇关于如何实现NSUserDefault以通过多个视图进行访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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