如何检测UISwitch是否打开/关闭?
我试图检测 UISwitch 何时打开/关闭
// .h
IBOutlet UISwitch *privateSwitch;
@property (nonatomic, retain) IBOutlet UISwitch *privateSwitch;
//.m
@synthesize privateSwitch;
privateSwitch = [[UISwitch alloc] init];
howToDisplay = @"no";
// In my cellForRowsAtIndexPath
UISwitch *privateSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(199, 8, 0, 0)];
[privateSwitch addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside];
[cell.contentView addSubview:privateSwitch];
if ([howToDisplay isEqualToString:@"no"]) {
[privateSwitch setOn:NO animated:NO];
} else {
[privateSwitch setOn:YES animated:NO];
}
- (void) switchToggled:(id)sender {
if ([privateSwitch isOn]) {
NSLog(@"its on!");
howToDisplay = @"yes";
[formDataTwo removeAllObjects];
[formTableView reloadData];
[privateSwitch setOn:YES animated:YES];
} else {
NSLog(@"its off!");
howToDisplay = @"no";
[formDataTwo removeAllObjects];
[formDataTwo addObject:@"Facebook"];
[formDataTwo addObject:@"Twitter"];
[formDataTwo addObject:@"Flickr"];
[formDataTwo addObject:@"Tumblr"];
[formDataTwo addObject:@"Email"];
[formDataTwo addObject:@"MMS"];
[formTableView reloadData];
[privateSwitch setOn:NO animated:YES];
}
}
但是,当我打开它时,它会说它已关闭。什么给?
谢谢。
I am trying to detect when a UISwitch it on / off
// .h
IBOutlet UISwitch *privateSwitch;
@property (nonatomic, retain) IBOutlet UISwitch *privateSwitch;
//.m
@synthesize privateSwitch;
privateSwitch = [[UISwitch alloc] init];
howToDisplay = @"no";
// In my cellForRowsAtIndexPath
UISwitch *privateSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(199, 8, 0, 0)];
[privateSwitch addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside];
[cell.contentView addSubview:privateSwitch];
if ([howToDisplay isEqualToString:@"no"]) {
[privateSwitch setOn:NO animated:NO];
} else {
[privateSwitch setOn:YES animated:NO];
}
- (void) switchToggled:(id)sender {
if ([privateSwitch isOn]) {
NSLog(@"its on!");
howToDisplay = @"yes";
[formDataTwo removeAllObjects];
[formTableView reloadData];
[privateSwitch setOn:YES animated:YES];
} else {
NSLog(@"its off!");
howToDisplay = @"no";
[formDataTwo removeAllObjects];
[formDataTwo addObject:@"Facebook"];
[formDataTwo addObject:@"Twitter"];
[formDataTwo addObject:@"Flickr"];
[formDataTwo addObject:@"Tumblr"];
[formDataTwo addObject:@"Email"];
[formDataTwo addObject:@"MMS"];
[formTableView reloadData];
[privateSwitch setOn:NO animated:YES];
}
}
However, when I switch it on, it will say it's off. What gives?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
cellForRowsAtIndexPath
方法中,您声明了一个隐藏实例变量privateSwitch
的局部变量UISwitch *privateSwitch
。在
switchToggled:
操作中,您使用实例变量来测试开关的状态,而不是在cellForRowAtIndexPath
中声明的状态。您可以像这样使用sender
参数:PS:我会使用
UIControlEventValueChanged
而不是UIControlEventTouchUpInside
。In your
cellForRowsAtIndexPath
method you are declaring a local variableUISwitch *privateSwitch
that is hiding your instance variableprivateSwitch
.In your
switchToggled:
action, you are using your instance variable to test the state of the switch, not the one declared incellForRowAtIndexPath
. You can use thesender
parameter like this:P.S: I would use
UIControlEventValueChanged
instead ofUIControlEventTouchUpInside
.