How to get the nsuserdefault value in other viewcontrollers

前端 未结 4 609
迷失自我
迷失自我 2020-12-25 09:08

I have two viewcontrollers. In my first viewcontroller:

{           
    NSString *string=textField.text;  
    NSUserDefaults *data = [NSUserDefaults standa         


        
4条回答
  •  醉梦人生
    2020-12-25 09:35

    Here you can use this in anyway in your application for store value of NSUserDefaults.

    // --- Saving
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    // saving an NSString
    [prefs setObject:@"TextToSave" forKey:@"keyToLookupString"];  
    // saving an NSInteger
    [prefs setInteger:42 forKey:@"integerKey"];
    // saving a Double
    [prefs setDouble:3.1415 forKey:@"doubleKey"];
    // saving a Float
    [prefs setFloat:1.2345678 forKey:@"floatKey"];
    // This is suggested to synch prefs, but is not needed (I didn't put it in my tut)
    [prefs synchronize];
    

    Here you can use this in anyway in your application for get value of NSUserDefaults.

    // --- Retrieving
    
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    // getting an NSString
    NSString *myString = [prefs stringForKey:@"keyToLookupString"];
    // getting an NSInteger
    NSInteger myInt = [prefs integerForKey:@"integerKey"];
    // getting an Float
    float myFloat = [prefs floatForKey:@"floatKey"];
    

提交回复
热议问题