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 6976375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:29:20+00:00 2026-05-27T17:29:20+00:00

I’m trying to create a push-on-push-off-like button with custom images in Xcode4 for iOS.

  • 0

I’m trying to create a push-on-push-off-like button with custom images in Xcode4 for iOS.
The code I’m using is

- (IBAction)btnAll:(id)sender
{
    UIButton *button = (UIButton *)sender;
    button.selected = !button.selected;
}

That works fine for now.
But my problem is, that when I’m toggling on, I press it on, then it is popping off again and then finally on.
The app works, but that is really ugly, though.

I firstly set the “highlighted” image to on. So when I highlight the button, it is on and that popping to on. That works fine. But when I turn it off again, the problem is the same, in the reverse direction.

I tried to put that code:

- (IBAction)btnAll:(id)sender
{
    UIButton *button = (UIButton *)sender;
    if(button.selected)
    {
        [button setImage[UIImage imageNamed@"off.png"] forState:UIControlStateHighlighted];
    }
    else
    {
        [button setImage[UIImage imageNamed@"on.png"] forState:UIControlStateHighlighted];
    }
    button.selected = !button.selected;
}

But as long button.selected = !button.selected there is no difference.
So it won’t make any change.

I also tried to trigger the IBAction on »Touch Down« but you can imagine how frustrating this will look like.

Has anybody got a solution for that problem?
Did anybody struggle with that one too?

Greets, thanks a lot
Julian

  • 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-27T17:29:21+00:00Added an answer on May 27, 2026 at 5:29 pm

    I’ve had a similar problem to this before, the button works a little strangely when tapping. Try this code and let me know if it works

        UIButton *button = (UIButton *)sender;
        if(button.selected)
        {
            [button setImage:[UIImage imageNamed:@"off.png"] forState:UIControlStateHighlighted];
            [button setImage:[UIImage imageNamed:@"off.png"] forState:UIControlStateSelected];
            [button setImage:[UIImage imageNamed:@"off.png"] forState:UIControlStateHighlighted | UIControlStateSelected];
        }
        else
        {
            [button setImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateHighlighted];
            [button setImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateSelected];
            [button setImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateHighlighted | UIControlStateSelected];
        }
        button.selected = !button.selected;
    
        If (button.selected) {
          [button setImage:[UIImage imageNamed:@"on.png"] forState:UIControlStateNormal];
        } else {
          [button setImage:[UIImage imageNamed:@"off.png"] forState:UIControlStateNormal];
        }
    

    When you tap and hold on a button the state is actually Highlighted & Selected so you need an image for both Highlight and selected state.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace

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.