1

基本上我试图让 nslog 在控制台中打开和关闭。但我似乎无法让它正常运行。作品很棒,NSUserDefaultsNSLog根本没有出现。

视图控制器 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) 添加的。尝试使用任何一种机制让您的代码正常工作。使用根据 Switch 打印 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 回答