2

我在其中设置了一个值,viewDidLoad因此它可以是默认设置,所以每当我加载到该视图控制器中时,它都会有一个默认值。

NSUserDefaults在这里使用:

- (void)viewDidLoad
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setInteger:1 forKey:@"defaultKey"];

    NSInteger defaultKey = [[NSUserDefaults standardUserDefaults] integerForKey:@"defaultKey"];

    if (defaultKey == 1){
    // do something 1
    } else if ( defaultKey == 2){
    // do something 2
    }
}

但我想要的是当按下按钮时,例如 button2,

-(IBAction) button2Pressed:(id)sender {
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setInteger:2 forKey:@"defaultKey"];
}

它会改变它的价值。因此,例如,我再次加载该 ViewController

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setInteger:2 forKey:@"defaultKey"];

将是下一次加载时的设置。

4

4 回答 4

2

这样做

- (void)viewDidLoad
{


    NSInteger defaultKey = [[NSUserDefaults standardUserDefaults] integerForKey:@"defaultKey"];

    if (defaultKey == 1){
    // do something 1
    } else if ( defaultKey == 2){
    // do something 2
    }
   else 
   {
        //for the first time
          NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setInteger:1 forKey:@"defaultKey"];
   } 
}

-(IBAction) button2Pressed:(id)sender {
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setInteger:2 forKey:@"defaultKey"];
}
于 2012-06-21T06:16:51.430 回答
0

试试这样

NSInteger defaultKey = [[NSUserDefaults standardUserDefaults] integerForKey:@"defaultKey"];
    if(defaultKey == 0){
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        [prefs setInteger:1 forKey:@"defaultKey"];
    }else if (defaultKey == 1){
        // do something 1
    } else if ( defaultKey == 2){
        // do something 2
    }
于 2012-06-21T06:19:53.320 回答
0

这没有意义。您应该首先将其设置为默认值(1 或 2)。老实说,您应该在 AppDelegate 上执行此操作:

  NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  // it will return 0 if it doesn't exist
  if([prefs integerForKey:@"defaultKey"] == 0){
       [prefs setInteger:1 forKey:@"defaultKey"];
  }

这样您就设置了默认值。之后,您可以将此逻辑保留在您的viewDidLoad

- (void)viewDidLoad
{
    NSInteger defaultKey = [[NSUserDefaults standardUserDefaults] integerForKey:@"defaultKey"];

    if (defaultKey == 1){
    // do something 1
    } else if ( defaultKey == 2){
    // do something 2
    }
}

然后在按下按钮时使用其余的逻辑:

-(IBAction) button2Pressed:(id)sender {
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setInteger:2 forKey:@"defaultKey"];
}
于 2012-06-21T06:20:47.467 回答
0

您需要做 2 件事:
1. 每次更新用户默认值以保留您的更改时,您应该这样做:

[prefs synchronize];

2.使用此代码:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger defaultKey = [prefs integerForKey:@"defaultKey"];
if(defaultKey == 0) //This is when you haven't set it and as it's integer you will get 0
{
    [prefs setInteger:1 forKey:@"defaultKey"];
    [prefs synchronize];
    //Do whatever you will do when it's 1
}
else if(defaultKey == 1)
{
   //Do something else
}
else if(defaultKey == 2)
{
  //Do something else
}


于 2012-06-21T06:23:45.660 回答