Search code examples
iosiphone-sdk-3.0uisegmentedcontrol

How to set selected segment index in UISegmentedControl?


I'm trying to avoid an app crash here... I have a button that will remove a segment from a UISegmentedControl. If that button is pressed and the user has the segment to be removed selected, the segment will remove and no selection will be highlighted. However, when another button is pushed that does an action that retrieves the selectedSegmentIndex, the app crashes.

In short: Is there any way to force the selection of a segment in a UISegmentedControl?

edit it seems as though the UISegmentedControl is returning a selectedSegmentIndex of -1 when no segment is selected... let's see what I can do from here.


Solution

  • Use yourSegmentname.selectedSegmentIndex = 1; or whichever segment you want.