2013-03-05 72 views
1

基本上,我试图让nslog在控制台中开启和关闭。但我似乎无法让它正常工作。 NSUserDefaults效果很好,但NSLog没有出现。为什么我的布尔函数无法正常工作Xcode

的ViewController 1的.h

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

的ViewController 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"); 
    } 
} 
+0

你有没有把'self.myVC.cameraSwitch'设置为任何东西?例如。把'self.myVC.cameraSwitch = onOffSwitch;'放在viewDidload中,甚至不要创建新的变量'onOffSwitch',只是实例化'cameraSwitch'。 – matsr 2013-03-05 00:37:40

+0

不属于你的问题,但如果你使用点语法访问属性,你应该使用属性名称,而不是getter名称:'if(cameraSwitch.on)'而不是'if(cameraSwitch.isOn)'。 – Sebastian 2013-03-05 01:01:54

回答

0

您已设置cameraSwitch有一个IBOutlet,然后你在初始化一个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 : %@",[email protected]"ON":@"OFF"); 

} 
0

它看起来像UISwitch(cameraSwitch)正在通过接口设计,以及通过代码(onOffSwitch)加入。尝试使用任何一种机制为您的代码工作。添加UISwitch使用代码打印NSLog消息取决于开关

- (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"); 
    } 
} 

希望这有助于。

相关问题