import UIKit
class ViewController: UIViewController ,UITextFieldDelegate,UITextViewDelegate,UIActionSheet Delegate,UIAlertViewDelegate{
override func viewDidLoad() {
super.viewDidLoad()
let items = ["选项一","选项二",UIImage(named: "lalala")!] as [AnyObject]
let segmented = UISegmentedControl (items: items)
segmented.frame = CGRectMake(10, 270, 200, 30)
segmented.selectedSegmentIndex = 1 //默认选中第二项
segmented.addTarget(self, action:"segmentDidchange:", forControlEvents: UIControlEvents.ValueChanged)//添加值改变监听
//添加选项
segmented.insertSegmentWithTitle("新加项目", atIndex: 1, animated: true)
//添加选项图片
segmented.insertSegmentWithImage(UIImage(named: "lalala"), atIndex: 2, animated: true)
//移除选项
segmented.removeSegmentAtIndex(1, animated: true)
segmented.tintColor = UIColor.redColor()
//修改选项文字
segmented.setTitle("swift", forSegmentAtIndex: 1)
segmented.setTitle("学习", forSegmentAtIndex: 1)
segmented.setImage(UIImage(named: "111"), forSegmentAtIndex: 3)
//修改选项便宜位置
segmented.setContentOffset(CGSizeMake(10, 7), forSegmentAtIndex: 0)
self.view.addSubview(segmented)
}
func segmentDidchange(segmented:UISegmentedControl){
//获取选项的索引
print(segmented.selectedSegmentIndex)
//获取选项的文字
print(segmented.titleForSegmentAtIndex(segmented.selectedSegmentIndex))
}
}
网友评论