基本上我正试图让nslog在控制台中打开和关闭。但我似乎无法让它正常运作。 NSUserDefaults
效果很好,但NSLog
根本没有出现。
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 :(得分: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 : %@",switchState?@"ON":@"OFF");
}
答案 1 :(得分: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");
}
}
希望这有帮助。