iPhone SDK: anybody has a clue why UIButton.highlighted does not seems to work ?

Discussion in 'iOS Programming' started by Nonnus3G, Jul 25, 2008.

  1. macrumors member

    Joined:
    Jul 9, 2008
    #1
    hi

    i have been trying to use ordinary round rect buttons to spare resources
    i guess a standard button is easier on the system than an image based one,
    am i wrong ?

    i was trying to use them as radio buttons or on off ones,
    where by clicking on a button it will get highlighted to indicate status

    however, although i can set the highlighed state of any other button,
    it does not work with the button that is being pressed,
    after unpressing it always gets back to non highlighted state

    is there a workaround ?

    also i cannot seem to find a way to colour a UIButton,
    it allows setting text color, background color, shadow but no button color

    anybody has a suggestion ?
    is there any way to achieve this or do i need to use image based buttons ?

    thanks in advance for your help

    nonnus
     
  2. macrumors 68000

    SPUY767

    Joined:
    Jun 22, 2003
    Location:
    GA
    #2
    I assume that you simply want them to appear as though they are highlighted when you have selected them? Essentially making them act like radio buttons, correct? If that is the case then I have a solution for you. It's just a few lines of code in your applicationDidFinishLaunhing method, and then you have to set the state of each button in it's target method, but that's easy enough.

    Use this image as your UIButton's backgroundImage: [​IMG]

    In your applicationDidFinishLaunching Method, add these lines (assuming three buttons):

    Code:
    //Sets the background images and title colors of the buttons to appear "highlighted" when they are selected
    	[UIButton1 setBackgroundImage:[UIImage imageNamed:@"ButtonBack.png"] forState:UIControlStateSelected];
    	[UIButton2 setBackgroundImage:[UIImage imageNamed:@"ButtonBack.png"] forState:UIControlStateSelected];
    	[UIButton3 setBackgroundImage:[UIImage imageNamed:@"ButtonBack.png"] forState:UIControlStateSelected];
    	[UIButton1 setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    	[UIButton2 setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    	[UIButton3 setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    
    You will also have to add lines to each of the target methods to set the current button's control state to selected via the UIButton.selected = YES/NO; method.

    Hope that helps.
     

    Attached Files:

  3. thread starter macrumors member

    Joined:
    Jul 9, 2008
    #3
    tx alot for replying

    i had already started to use image based custom buttons
    and have been using the selected property to set them as radio buttons
     
  4. macrumors member

    Joined:
    Mar 25, 2002
    #4
    radio buttons...

    The closest thing to a radio button on the iPhone is the UISegmentedControl. It holds state--works very well.
     
  5. macrumors newbie

    Joined:
    Feb 3, 2009
  6. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    I believe the HIG recommends against leaving a UIButton selected to represent toggle state, in favor of using UISwitch.
     

Share This Page