0

ユーザーが設定を変更するオプションを提供する設定ビューがあります。

設定ビューにはtable view2がありUISwitchesます。NSUserdefaults状態を保存する方法と、値をロードするコードの書き方について助けが必要です。

これまでの私のコードは次のとおりです

In `cellForRowAtIndexPath` method:

      [cell.switchButton addTarget:self action:@selector(switchButtonTapped:) forControlEvents:UIControlEventValueChanged];



    [[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"myBool"];
    BOOL hBool = [[ NSUserDefaults standardUserDefaults] boolForKey:@"myBool"];

    NSLog(@"tag %i", tappedSwitch.tag);

    if (cell.switchButton.tag == 0) {
        [cell.switchButton setOn:hBool];

    }    
return cell;
}



    - (void) switchButtonTapped: (id) sender {

            tappedSwitch = (UISwitch *) sender;
        switch (tappedSwitch.tag) {
            case 0:
                passcodeSwitchIsOn = tappedSwitch.isOn;
                if (passcodeSwitchIsOn) {

                    GCPINViewController *PIN = [[GCPINViewController alloc]
                                                initWithNibName:nil
                                                bundle:nil
                                                mode:GCPINViewControllerModeCreate];
                    PIN.messageText = @"Enter a passcode";
                    PIN.errorText = @"The passcodes do not match";
                    PIN.title = @"Set Passcode";
                    PIN.verifyBlock = ^(NSString *code) {
                    NSLog(@"setting code: %@", code);
                    code = saveString; 
                    return YES;
                        };
                    [PIN presentFromViewController:self animated:YES];
                    [PIN release];
     // method of saving                    
 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
                [defaults setBool:passcodeSwitchIsOn forKey:@"myBool"];
                [defaults synchronize];
                }

                break;
            case 1:
                bluetoothSwitchIsOn = tappedSwitch.isOn;
                if (bluetoothSwitchIsOn) {

                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Bluetooth Switch" message:@"Bluetooth Switch is ON" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
                    [alert show];
                    [alert release];

                }

                break;

            default:
                break;
        }
            [settingsTable reloadData];
    }
4

2 に答える 2

3

ユーザーのデフォルトへの参照を作成します。

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

値を設定します。

[defaults setBool:1 forKey:@"bluetooth"];

Bluetooth がオフの場合は、これを 0 に戻すことができます。

[defaults setBool:0 forKey:@"bluetooth"];

ユーザーのデフォルトは、選択した文字列によって識別されます。この場合: @"bluetooth". デフォルトの値は、作成して別の値に設定するまで nil です。

したがって、次のように言えます。

if (!bluetooth) // bluetooth is off
else  // bluetooth is on

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

于 2013-01-29T11:39:04.697 に答える
2

これは、問題の処理に使用できます。

IBOutlet UISwitch * swi_yourswitch;
@property(nonatomic,retain)  UISwitch * swi_yourswitch;

含意

@synthesise swi_yourswitch;

- (void) switchButtonTapped: (id) sender {
    UISwitch * switchObj = (UISwitch*)sender;
    if(switchObj == self.swi_yourswitch){
        if (self.swi_yourswitch.on){

            NSLog(@"On");
        }
        else{
            NSLog(@"Off");
            [self.swi_yourswitch setOn:0];
        }
    }

価値を保存するには

[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:@"mybool"];
[[NSUserDefaults standardUserDefaults] synchronize];

プリペイドカードを取得するには

[NSUserDefaults standardUserDefaults] valueForKey:@"mybool"]
于 2013-02-02T10:06:10.520 に答える