1

基本的に、コンソールで nslog のオンとオフを切り替えようとしています。しかし、正しく機能させることができないようです。NSUserDefaultsうまく機能しますが、まったくNSLog表示されません。

ビューコントローラー 1 .h

@property (nonatomic, weak) IBOutlet UISwitch *cameraSwitch;

ビューコントローラー1.m

- (void)cameraEnabled
{
    if (cameraSwitch.isOn)
    { 
    }
    else
    {       
    }
}

ビュー コントローラー 2.h

 viewcontroller1 *myVC;
 @property (nonatomic, retain) IBOutlet viewcontroller *myVC;

ビューコントローラー2.m

- (void)viewDidLoad
{
    UISwitch *onOffSwitch = [[UISwitch alloc] init];
    /* If it is the first time were are running this code, we will get nil from
       NSUserDefaults, and we'll turn the switch off.
       After the user has set the switch, it will store the value in NSUserDefaults
       and we can remember what to set it to the next time viewDidLoad is called.
    */
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"SwitchKey"]) {
        [onOffSwitch setOn:YES animated:NO];
    } else {
        [onOffSwitch setOn:NO animated:NO];
    }

    [self.myVC.cameraSwitch addTarget:self action:@selector(openswitch) forControlEvents:UIControlEventValueChanged];
}

- (void)openswitch
{ 
    if (self.myVC.cameraSwitch.isOn)
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"SwitchKey"];
        NSLog(@"on");  
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"SwitchKey"];
         NSLog(@"off"); 
    }
}
4

2 に答える 2

0

IBOutlet で cameraSwitch を設定してから、viewDidLoad: で newSwith を初期化しています。xibでIBOutletを正しく接続した場合。次の変更が必要です

- (void)viewDidLoad
{
    //No need to initialize switch again
    //UISwitch *onOffSwitch = [[UISwitch alloc] init];
    /* If it is the first time were are running this code, we will get nil from
       NSUserDefaults, and we'll turn the switch off.
       After the user has set the switch, it will store the value in NSUserDefaults
       and we can remember what to set it to the next time viewDidLoad is called.
    */

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL switchState = [defaults boolForKey:@"SwitchKey"];
    [self.cameraSwitch setOn:switchState animated:NO];

    [self.cameraSwitch addTarget:self action:@selector(openswitch) forControlEvents:UIControlEventValueChanged];
}

- (void)openswitch
{ 

    BOOL switchState = self.cameraSwitch.isOn;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setBool:switchState forKey:@"SwitchKey"];
    [defaults synchronize];

    NSLog(@"Camera Swith State : %@",switchState?@"ON":@"OFF");

}
于 2013-03-05T01:03:59.320 に答える
0

UISwitch (cameraSwitch) は、インターフェイス デザイナーとコード (onOffSwitch) を介して追加されているようです。コードが機能するように、いずれか 1 つのメカニズムを使用するようにしてください。スイッチに応じて NSLog メッセージを出力するコードを使用して UISwitch を追加する例

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UISwitch *onOffSwitch = [[UISwitch alloc] init];
    /* If it is the first time were are running this code, we will get nil from
     NSUserDefaults, and we'll turn the switch off.
     After the user has set the switch, it will store the value in NSUserDefaults
     and we can remember what to set it to the next time viewDidLoad is called.
     */
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"SwitchKey"]) {
        [onOffSwitch setOn:YES animated:NO];
    } else {
        [onOffSwitch setOn:NO animated:NO];
    }
    [onOffSwitch addTarget:self action:@selector(openswitch:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:onOffSwitch];

}

-(IBAction)openswitch:(id)sender
{
    UISwitch *onOffSwitch = (UISwitch *) sender;
    if (onOffSwitch.isOn)
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"SwitchKey"];
        NSLog(@"on");
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"SwitchKey"];
        NSLog(@"off");
    }
}

お役に立てれば。

于 2013-03-05T01:08:14.387 に答える