tags:

views:

2496

answers:

5

Hi,

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 could subclass an UIButton. That way I do not have to implement touch handling. I could just create a method that returns a boolean indicating whether the button is in on or off state.

I could also start with an UIView subclass and create it from scratch. I suppose it would be easier to do things like a transition animation, but I would have to create my own touch handling.

What do you think is the best one? Or would you know a better way? (Maybe there is an open-source/free view available on the web?)

EDIT: I don't want to use the UISwitch because I want my toggle button to look like that. I want it to be a big rectangular button and make the background change to indicate the state.

Thanks!

A: 

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.

Cinder6
`UISwitch` is a way to implement toggling
Perspx
Thanks. I couldn't remember the exact name, and I don't have the iPhone SDK installed on this system.
Cinder6
A: 

May be i dont understand the question properly ..why are you not using UISwitch

Surya
A: 

You can use UIButton and set its enabled property.

In Interface Builder, you can set separate images for the enabled and disabled states, and the IBAction change the property.

pgb
That's clever. Thank you!
Jongsma
I was wondering why you chose the `enabled` property instead of `selected`? After reading this tip a while back, I implemented it today using `selected`. Works great. Then I reread the tip and tried `enabled`. Once disabled, you can no longer click the button, so it is not a great toggle. Is there some setting I missed?
slothbear
A: 

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.

Mike
A: 

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?

Lee Probert
Yes, so what I ended up doing is using the 'selected' property, which - as far as I know - doesn't really do anything for UIButton's.
Jongsma
Does the UIButton support all the toggle button states for skinning. Forgive my ignorance as I am looking at this as a Flex developer. In Flex a ToggleButton has the usual up, over, disabled states but then also has selectedUp, selectedOver, selectedDisabled etc. You would need all these states to create a proper toggle. And yes, you also need a selected property. If this exists on the UIButton it would suggest you can activate toggling. Is this so?
Lee Probert
No, I don't believe it has. But what you can do is overwrite the setSelected: method and change the styling in there.
Jongsma