UISwitch 对值变化执行操作-iphone sdk
我有一个 UISwitch,当将开关状态从 OFF 更改为 ON 时,我需要将字符串中的值加载到数组中。我使用了以下代码,但不起作用。
-(IBAction)toggleSwitch1:(id)sender {
if (addSwitch.on) {
[addSwitch addTarget:self action:@selector(addName:) forControlEvents:UIControlEventValueChanged];
NSLog(@"%@",userName);
}else {
NSLog(@"No");
}
}
-(void)addName:(UIControl*)sender {
[alertList addObject:userName];
NSLog(@"AlertList: %@", alertList);
}
如果状态更改为 ON 后,如果我重新加载页面,如何保持相同的状态?
请分享您的想法。 谢谢
I have a UISwitch
and I need to load the value in a string into an array when change the switch state change to OFF to ON. I have used the following code, but not working.
-(IBAction)toggleSwitch1:(id)sender {
if (addSwitch.on) {
[addSwitch addTarget:self action:@selector(addName:) forControlEvents:UIControlEventValueChanged];
NSLog(@"%@",userName);
}else {
NSLog(@"No");
}
}
-(void)addName:(UIControl*)sender {
[alertList addObject:userName];
NSLog(@"AlertList: %@", alertList);
}
And if once the state changed to ON, how can I remain the same state, if I reload the page?
Please share your thoughts.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您遇到的问题是当状态更改时调用toggleSwitch 方法,然后它开始使用addTarget: 方法监听更改。您应该选择其中之一来处理更改。
要保存开关状态,只需使用另一个变量来存储开关状态并调用 viewDidLoad 中的 setOn:animated: 方法。
Looks like the issue you're having is that the toggleSwitch method is called when the state changes and then it starts listening for the change using the addTarget: method. You should choose one or the other to handle the change.
To save the switch state just have another variable that stores the switch state and call the setOn:animated: method in viewDidLoad.
在
viewDidLoad
中使用这个:In
viewDidLoad
use this one: