【问题标题】:How to tell when a UISwitch inside of a UITableViewCell has been tapped?如何判断何时点击了 UITableViewCell 内的 UISwitch?
【发布时间】:2010-03-27 07:33:14
【问题描述】:

如何判断UITableViewCell 内的UISwitch 何时被窃听?

我的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 只需轻按一下即可关闭开关?我该如何解决?

【问题讨论】:

    标签: iphone xcode uitableview uiswitch


    【解决方案1】:

    给开关一个目标和动作:

    [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
    }
    

    【讨论】:

    • 谢谢。我没有使用标签属性,而是为每个开关创建了一个特定的操作(switchToggled1、switchToggled2)。但是有一个问题:当我点击一次开关时,由于某种原因,该动作被调用了两次。为什么会被调用两次? (更新:关闭开关调用动作两次,打开开关只调用一次动作)
    • 在上面的示例代码中应该是@selector 而不是@selection。这花了我大约半小时的时间来弄清楚。
    • @Chuck 哎呀!对于那个很抱歉。这就是在浏览器中输入代码的缺点...
    【解决方案2】:

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

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

    我没有这样的麻烦。

    【讨论】:

    • 您可能还想在@selector 中使用valueChanged:
    【解决方案3】:

    这解决了为什么 switchToggled 被称为 TWICE 的原因吗?也发生在我身上。它记录了两次 NSLog。但就我而言,它是随机的。有时关闭它被调用两次,有时打开。附上日志

    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
    

    【讨论】:

    • 这个问题解决了吗?我发现[table reloadData]switchChanged: 方法的末尾会触发我的switchChanged: 方法两次。
    猜你喜欢
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 2022-08-19
    • 1970-01-01
    相关资源
    最近更新 更多