2013-03-05 102 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"); 
    } 
} 

希望這有助於。

相關問題