Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 698661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:18:00+00:00 2026-05-14T03:18:00+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:18:01+00:00Added an answer on May 14, 2026 at 3:18 am

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Tell me if this is a) possible and b) allowed. I'd like to build
Please tell me if I got this wrong. Anchor point is like a movable
I've got a little problem in my UITableViewCell. Each Custom Cell have an UISwitch
Plz tell me where i'm doing wrong... I've 3 classes. These are like this..
Tell me please, why this sample code doesn't work on ASP.NET MVC? http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.onselectedindexchanged.aspx Thank
Please tell me why i am getting ClassCastException in this case I have type
Please tell me how to store the level information (like angry birds levels display
tell application Adobe Photoshop CS5.1 set myFile to (choose file) as string open file
Tell me please why this function may take '\0' as second argument, compile and
Or tell me if I'm misunderstanding what I'm seeing, but check out this link.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.