swift3 分段选择控件

let sitems = ["0", "1", "2", "3", "4"]
let segment = UISegmentedControl(items: sitems)
self.view.addSubview(segment)
segment.center = self.view.center
segment.selectedSegmentIndex = 1 // 默认选中第二项
// 颜色
//segment.tintColor = UIColor.red
//添加值改变监听
segment.addTarget(self, action: #selector(segmentDidchange(_:)), for: .valueChanged)
    func segmentDidchange(_ segmented:UISegmentedControl){
        //获得选项的索引
        print(segmented.selectedSegmentIndex)
        //获得选择的文字
        print(segmented.titleForSegment(at: segmented.selectedSegmentIndex))
    }

你可能感兴趣的:(swift3 分段选择控件)