xcode 如何判断 UITableViewCell 内的 UISwitch 何时被点击?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2528535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to tell when a UISwitch inside of a UITableViewCell has been tapped?
提问by RexOnRoids
How to tell when a UISwitch
inside of a UITableViewCell
has been tapped?
如何判断 a 的UISwitch
内部何时UITableViewCell
被窃听?
My UISwitch
is set up inside of the cell (generic cell) like this:
我UISwitch
的设置在单元格(通用单元格)内部,如下所示:
UISwitch *mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
[cell addSubview:mySwitch];
cell.accessoryView = mySwitch;
And I am trying to detect a tap like this (but its not working):
我正在尝试检测这样的水龙头(但它不起作用):
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
NSUserDefaults *prefs;
if(indexPath.section == 1){
switch(indexPath.row)
{
case 0:
NSLog(@"Tapped Login Switch");
break;
default:
break;
}
}
}
Dave DeLongsuggested that I set an action for each switch as a solution. So I did the following to set the switch:
Dave DeLong建议我为每个开关设置一个动作作为解决方案。所以我做了以下设置开关:
UISwitch *mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
[mySwitch addTarget:self action:@selector(switchToggled2:) forControlEvents: UIControlEventTouchUpInside];
if(at_songs){
[mySwitch setOn:YES animated:NO];
}
[cell addSubview:mySwitch];
cell.accessoryView = mySwitch;
And the following to know when it was tapped:
以及了解何时被点击的以下内容:
-(IBAction)switchToggled1:(id)sender {
NSUserDefaults *prefs;
NSLog(@"Tapped Login Switch");
prefs = [NSUserDefaults standardUserDefaults];
if(at_login){
[prefs setObject:@"NO" forKey:@"autotweet_login"];
at_login = NO;
}else{
[prefs setObject:@"YES" forKey:@"autotweet_login"];
at_login = YES;
}
}
Turning the switch ON is not a problem. The problem NOW is that when the UISwitch is set to OFF, for some reason its action gets called twice (And I get 2 NSLogs for 1 tap).
打开开关不是问题。现在的问题是,当 UISwitch 设置为 OFF 时,由于某种原因,它的操作会被调用两次(我得到 2 NSLogs for 1 tap)。
Why is the action getting called TWICE for only one tap to turn the switch OFF? How do I fix it?
为什么只需轻轻一按即可关闭开关,该操作会被称为 TWICE?我如何解决它?
回答by Dave DeLong
Give the switch a target and action:
给 switch 一个目标和动作:
[mySwitch addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside];
Then implement your switchToggled: method:
然后实现你的 switchToggled: 方法:
- (void) switchToggled:(id)sender {
//a switch was toggled.
//maybe use it's tag property to figure out which one
}
回答by Grant M
for people having trouble with multiple touches have you tried changing the control event to UIControlEventValueChanged
对于多次触摸有问题的人,您是否尝试将控件事件更改为 UIControlEventValueChanged
[catSwitch addTarget:self action:@selector(catSwitched:) forControlEvents: UIControlEventValueChanged];
I am not having troubles this way.
这样我就没有麻烦了。
回答by Dev
Is that resolved why the switchToggled is called TWICE? Happening to me as well. Its logging the NSLog twice. But in my case its random. Sometimes on OFF it is called twice and sometimes ON. Attaching the log
这是否解决了为什么 switchToggled 被称为 TWICE 的问题?也发生在我身上。它记录了两次 NSLog。但就我而言,它是随机的。有时在 OFF 时调用两次,有时在 ON 时调用。附上日志
2010-08-17 18:12:30.264 SimplyPersonnelV1[3190:207] Auto Login turned on
2010-08-17 18:12:33.032 SimplyPersonnelV1[3190:207] Auto Login turned off
2010-08-17 18:12:33.032 SimplyPersonnelV1[3190:207] Auto Login turned off
2010-08-17 18:12:33.760 SimplyPersonnelV1[3190:207] Auto Login turned on
2010-08-17 18:12:46.223 SimplyPersonnelV1[3190:207] Auto Login turned off
2010-08-17 18:12:47.383 SimplyPersonnelV1[3190:207] Auto Login turned on
2010-08-17 18:12:48.000 SimplyPersonnelV1[3190:207] Auto Login turned off
2010-08-17 18:12:48.623 SimplyPersonnelV1[3190:207] Auto Login turned on
2010-08-17 18:12:49.176 SimplyPersonnelV1[3190:207] Auto Login turned off
2010-08-17 18:12:59.687 SimplyPersonnelV1[3190:207] Auto Login turned on
2010-08-17 18:12:59.688 SimplyPersonnelV1[3190:207] Auto Login turned on
2010-08-17 18:13:00.246 SimplyPersonnelV1[3190:207] Auto Login turned off
2010-08-17 18:13:00.759 SimplyPersonnelV1[3190:207] Auto Login turned on
2010-08-17 18:13:00.759 SimplyPersonnelV1[3190:207] Auto Login turned on
2010-08-17 18:13:05.638 SimplyPersonnelV1[3190:207] Auto Login turned off
2010-08-17 18:13:06.391 SimplyPersonnelV1[3190:207] Auto Login turned on
2010-08-17 18:13:06.391 SimplyPersonnelV1[3190:207] Auto Login turned on
2010-08-17 18:13:07.078 SimplyPersonnelV1[3190:207] Auto Login turned off
2010-08-17 18:13:07.830 SimplyPersonnelV1[3190:207] Auto Login turned on
2010-08-17 18:13:07.830 SimplyPersonnelV1[3190:207] Auto Login turned on
2010-08-17 18:13:08.622 SimplyPersonnelV1[3190:207] Auto Login turned off
2010-08-17 18:13:09.261 SimplyPersonnelV1[3190:207] Auto Login turned on
2010-08-17 18:13:09.262 SimplyPersonnelV1[3190:207] Auto Login turned on
2010-08-17 18:13:15.565 SimplyPersonnelV1[3190:207] Auto Login turned off
2010-08-17 18:13:16.485 SimplyPersonnelV1[3190:207] Auto Login turned on
2010-08-17 18:13:16.486 SimplyPersonnelV1[3190:207] Auto Login turned on