views:

1926

answers:

5

Trying to do simple button functionality.

Button A. It has 3 states: default, highlighted, selected. Should UNSELECT when clicked again. For the life of me, I can't even get a simple 3 state functionality established.

Highlight (on press) appears built in (goes to "stock" blue). I"ve used Button Attributes to load in image for selected state...PLayed with Control/Content to click Highlight and Selected on and off, trying to find the right combo...

I thought it'd simply be selecting in dropdown the state I want to edit....and it would register my edits for that state...images loaded/ colors changed/ etc....

NO!!!! What am I missing..?

A: 

What are you trying to do set the images or titles for the different states? For that you can just use methods such as - (void)setImage:(UIImage *)image forState:(UIControlState)state, or - (void)setTitle:(NSString *)title forState:(UIControlState)state...is that what you are asking? maybe i am misunderstanding your question its not that clear

Daniel
A: 

You really should post some code (or an IB screenshot) as it's hard to fully parse what you are saying.

The default behavior of a UIButton is to show the Selected state on press, then revert back to the normal state. If you want it to "lock down" a press, you need to do something like this in the IBAction hooked to the UIButton:

BOOL pressed;
UIButton *button

- (IBAction) toggleButton:(id)sender
{
    button.selected = ! pressed;
    pressed = ! pressed;
}
Kendall Helmstetter Gelner
A: 

AH! Well, not being a programmer, I was hoping for simple "stock" solution within Interface Builder.

To be clear of the "want/wish:

Button A-E. Each button should have 3 states: Default, highlight, selected. Default: resting state. Highlight: some treatment using interface builder features or import a graphic that will appear on Highlight. This state appears while button is being pressed. Selected: when button A-E is released, it shows that it has been selected by displaying a new state, again by using tweaks within interface builder (font color, shadow) or import a graphic. When you click the SELECTED button again, it returns to Default state.

I guess I just thought it'd be "easy" because SDK 3.0 Interface Builder has dropdowns for the 3 states. I thought code would "magically" be assigned to the button allowing it to function as desired along with it's aesthetic shift.

I TOTALLY appreciate any and all feedback. I can pass along any coding advice to partner on this App.

A: 

I couldnt find a straight answer on this anywhere. So I brewed my own solution that seems to work great.

Dont forget to wire up your buttons in IB you will need to bind both

"touch up inside" to "setButtonPressed"

"file owner" to "button" ex. 1stButton


Psuedo Code

-Clear all buttons selected

-Set sender to selected

CODE


//Declare your buttons in .h

UIButton *1stButton; UIButton *2ndButton; UIButton *3rdButton;

@property(nonatomic, retain) IBOutlet UIButton *1stButton;

@property(nonatomic, retain) IBOutlet UIButton *2ndButton;

@property(nonatomic, retain) IBOutlet UIButton *3rdButton;


//In .m file - write 2 methods

-(void)clearButtons {

[1stButton setSelected:FALSE];
[2ndButton setSelected:FALSE];
[3rdButton setSelected:FALSE];

}

//attach to touch up inside event in IB for each button

-(void) setButtonPressed:(UIButton *)sender {

self.clearButtons;
[sender setSelected:TRUE];
[sender setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

}

grimper
A: 

Try this:

UIImage *img1 = [UIImage  imageNamed:@"image1.png"];
UIImage *img2 = [UIImage  imageNamed:@"image2.png"];
UIImage *img3 = [UIImage  imageNamed:@"image3.png"];

[button setImage:img1 forState:UIControlStateNormal];
[button setImage:img2 forState:UIControlStateHighlighted];
[button setImage:img3 forState:UIControlStateSelected];
[button setImage:img2 forState:(UIControlStateHighlighted+UIControlStateSelected)];

[img1 release];
[img2 release];
[img3 release];

This is because the state variable is actually a bit flag.

Ducksauce