How to set selected segment index in UISegmentedControl?

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


This code is for swift 2.0

@IBOutlet weak var segmentcontroll: UISegmentedControl!
    @IBAction func segmentneeded(sender: AnyObject)
        {

            if(segmentcontroll.selectedSegmentIndex==0)
            {
                self.view.backgroundColor=UIColor.purpleColor()
                segmentcontroll.selectedSegmentIndex=UISegmentedControlNoSegment
            }
            else if(segmentcontroll.selectedSegmentIndex==1)
            {
                    self.view.backgroundColor=UIColor.yellowColor()
                            segmentcontroll.selectedSegmentIndex=UISegmentedControlNoSegment
            }
            else
            {
                self.view.backgroundColor=UIColor.grayColor()
                            segmentcontroll.selectedSegmentIndex=UISegmentedControlNoSegment
            }
        }