views:

668

answers:

4

How to tell when a UISwitch inside of a UITableViewCell has been tapped?

My UISwitch is set up inside of the cell (generic cell) like this:

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 DeLong suggested that I set an action for each switch as a solution. So I did the following to set the switch:

        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).



Why is the action getting called TWICE for only one tap to turn the switch OFF? How do I fix it?

+1  A: 

Give the switch a target and action:

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

Then implement your switchToggled: method:

- (void) switchToggled:(id)sender {
  //a switch was toggled.  
  //maybe use it's tag property to figure out which one
}
Dave DeLong
Thanks. Instead of using tag property, I created a specific action (switchToggled1, switchToggled2) for each switch. But there is one problem: When i tap the switch once, for some reason the action is called twice. Why would it be getting called twice?? (UPDATE: Turning the switch OFF calls the action twice, turning the switch ON calls the action only once)
RexOnRoids
That should be @selector NOT @selection in the sample code above. That cost me about a half hour of my time to figure out.
Chuck Smith
@Chuck whoops! sorry about that. That's the downside of typing code in a browser...
Dave DeLong
A: 

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

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
Dev
A: 

Has anyone resolved this? I'm seeing similar called-twice bad behavior but oddly the bad behavior is only seen on the device--the simulator works perfectly.

A: 

for people having trouble with multiple touches have you tried changing the control event to UIControlEventValueChanged

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

I am not having troubles this way.

Grant M