如何判断UITableViewCell内的UISwitch何时被点击?

时间:2010-03-27 07:33:14

标签: iphone xcode uitableview uiswitch

如何判断UISwitch内的UITableViewCell何时被点击?

我的UISwitch设置在单元格(通用单元格)内部,如下所示:

UISwitch *mySwitch = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
[cell addSubview:mySwitch];
cell.accessoryView = mySwitch;

我正试图检测这样的水龙头(但它不起作用):

- (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 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;



以下是知道它被点击的时间:

-(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;
    }



}

打开开关不是问题。现在的问题是,当UISwitch设置为OFF时,由于某种原因,它的动作被调用两次(并且我获得2个NSLogs 1次)。



为什么只有一个水龙头将动作调到TWICE才能关闭开关?我该如何解决?

3 个答案:

答案 0 :(得分:12)

为交换机提供目标和操作:

[mySwitch addTarget:self action:@selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside];

然后实现你的switchToggled:方法:

- (void) switchToggled:(id)sender {
  //a switch was toggled.  
  //maybe use it's tag property to figure out which one
}

答案 1 :(得分:11)

对于多次触摸有问题的人,您尝试将控制事件更改为UIControlEventValueChanged

[catSwitch addTarget:self action:@selector(catSwitched:) forControlEvents: UIControlEventValueChanged];

我没有这样的麻烦。

答案 2 :(得分:1)

这解决了为什么switchToggled被称为TWICE?也发生在我身上。它记录NSLog两次。但在我的情况下,它是随机的。有时在OFF时它会被调用两次,有时也会打开。附加日志

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