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

  • Home
  • SEARCH
  • 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 8623505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:17:50+00:00 2026-06-12T07:17:50+00:00

How should I go about this? I have a UISwitch. When the user taps

  • 0

How should I go about this?

I have a UISwitch. When the user taps the switch, something amazing happens!

Now, if the user double taps the switch, I haver an amazing crash! Because the first tap was still doing amazing things!

What I’m trying to do is this,

USER TAPS:

1 – toggleOvertimeSwitch is called

2 – Removes target from switch (in order to avoid being called if a double tap happens)

3 – Do amazing things

4 – Add target again

Although there are a few concerns to take care about the Switch position later on, at first glance, this should work. But it doesn’t.

I am removing myself as the target, but it still keeps responding the all the taps! toggleOvertimeSwith is always being called. I don’t understand why.

Can anyone help me figure this out please?

Thank you!

Nuno

Code:

My viewDidLoad implements this,

[self.toggleSwitch addTarget:self
                      action:@selector(toggleOvertimeSwitch:)
            forControlEvents:UIControlEventValueChanged];

And my toggleOvertimeSwitch implements this,

-(void)toggleOvertimeSwitch:(UISwitch *)sender
{

[self.toggleSwitch removeTarget:self
                         action:@selector(toggleOvertimeSwitch:)
               forControlEvents:UIControlEventValueChanged];

// Do something amazing here


[self.toggleSwitch addTarget:self
                      action:@selector(toggleOvertimeSwitch:)
            forControlEvents:UIControlEventValueChanged];

}
  • 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-06-12T07:17:51+00:00Added an answer on June 12, 2026 at 7:17 am
    • If your part mentioning “do something amazing” is executed in the same thread/queue as the rest of your code, it won’t change anything, as every event / use action is handled in the main thread, so from the beginning of your toggleOvertimeSwitch: to its end, there won’t be any interruption or any other call of this method triggered by any user action before the method itself is finished.

    • If your “amazing thing” code is executed in a separate thread, for example using the GCD dispatch_async function, then it is meaningful to disable your switch action, but:

      • you should wait for your “amazing work” to complete before reactivating your switch actions, as if you really do something asynchronously in there, and the code re-adding your target/action will be executed without waiting for the asynchronous code to complete, your switch will be re-enabled before the action is completed, by definition
    • and still, you should use the enabled property of your switch to disable and re-enable it, instead of removing and re-adding the target/action, it would be much more easy!

    I don’t know if you really use GCD or some threading code or whatever to do your “amazing work”, as you didn’t give much info in your question about that, so I’m just completely guessing, but if you use dispatch_async for example, it would give sthg like this:

    -(IBAction)toggleOvertimeSwitch:(UISwitch*)sender
    {
        // disable the switch
        sender.enabled = NO;
    
        // start your long background work
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_NORMAL, 0), ^{
          // ... some amazing work in the background, executed asynchronously ...
    
          // Then, when finished, re-enable the switch:
          dispatch_sync(dispatch_get_main_queue(), ^{
            sender.enabled = YES;
          });
        });
    }
    

    PS : Again, if you don’t use any threading nor asynchronous code that gets executed in the background, but use code that executes synchronously in the current thread, then your problem does not exist at all as all your toggleOvertimeSwitch: method will only be executed in one step without interruption, and the next UI event (touch, etc) will only be processed after all that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already asked a question about this yesterday, but now I have another :)
I just learned about ThreadLocal this morning. I read that it should always be
Two quick questions if I may, is this how I should go about taking
I'm working on a project in C#, it's about E-learning.This project should use plug-ins
I'm trying to learn this basic thing about processors that should be taught in
Ok i have this function. what it should be doing it looking for pid
I have read a ton about this problem, but I cannot figure it out.
I know there are lots of stack overflow posts about this. I have this
I was always wondering about this and today I have finally came to a
I have three activities A,B & C.Now the Task i have this form A,B

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.