I would like to create a toggle button in my iPhone application. However, I don\'t know exactly what would be the best approach for this.
I am considering two options.
I too looked at UISwitch, but in my case I do not want an on/off button, I really want a toggle button. What I am trying to do, again in my case, is have a block of buttons that look like a UISegmentedControl, but where the user can "toggle" more than one of the buttons on the control at one time. The issue I am trying to solve is that I have VERY limited screen realestate and a lot of toggles to get on the screen.
My fall-back is probably going to be a UITableView full of toggle cells. This way I can add as many toggles as I want in a fixed, scrollable area of the screen.
Sadly, UISwitch does not always fit the UI specs people have in mind, so I am guess the gentleman that posted the original post was not looking for an On/Off switch either, but rather wanted a button that changed its state somehow.
Just make an (IBAction) method and also a BOOL. Set the BOOL default to FALSE. Your IBAction must look like this:
- (IBAction) .... {
if (bool) {
// do your things
}
else {
// do your things
}
bool =! bool
}
That works for me!
I need to do this too. Wouldn't setting the enabled state to NO prevent the button being toggled back though?
UPDATE
People are suggesting UISwitch but can this be skinned with all the relevant up,over,disabled,selectedUp,SelectedOver,selectedDisabled states?
-(IBAction)toggle{
if (button.selected == NO) {
button.selected = YES;
}
else {
button.selected = NO;
}
}
or
-(IBAction)toggle:(UIButton*)sender
{
sender.selected = !sender.selected;
}
simple dude
Isn't there already a toggle button, or am I missing something here?
EDIT: As others have said, it's called UISwitch
, and it toggles on and off.
May be i dont understand the question properly ..why are you not using UISwitch