如何检测UISwitch是否打开/关闭?

发布于 2024-11-10 13:14:08 字数 1293 浏览 4 评论 0原文

我试图检测 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↙温凉少女 2024-11-17 13:14:08

cellForRowsAtIndexPath 方法中,您声明了一个隐藏实例变量 privateSwitch 的局部变量 UISwitch *privateSwitch

switchToggled: 操作中,您使用实例变量来测试开关的状态,而不是在 cellForRowAtIndexPath 中声明的状态。您可以像这样使用 sender 参数:

- (void) switchToggled:(id)sender {
    UISwitch *mySwitch = (UISwitch *)sender;
    if ([mySwitch isOn]) {
        NSLog(@"its on!");
    } else {
        NSLog(@"its off!");
    }
}

PS:我会使用 UIControlEventValueChanged 而不是 UIControlEventTouchUpInside

In your cellForRowsAtIndexPathmethod you are declaring a local variable UISwitch *privateSwitch that is hiding your instance variable privateSwitch.

In your switchToggled: action, you are using your instance variable to test the state of the switch, not the one declared in cellForRowAtIndexPath. You can use the sender parameter like this:

- (void) switchToggled:(id)sender {
    UISwitch *mySwitch = (UISwitch *)sender;
    if ([mySwitch isOn]) {
        NSLog(@"its on!");
    } else {
        NSLog(@"its off!");
    }
}

P.S: I would use UIControlEventValueChanged instead of UIControlEventTouchUpInside.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文