基本上我试图让 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");
}
}