How to set selected segment index in UISegmentedControl?

esqew picture esqew · Feb 10, 2010 · Viewed 53.2k times · Source

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.

Answer

neillo picture neillo · Jun 9, 2010

Use like this.. yourSegmentname.selectedSegmentIndex = 1; // or whichever segment you want