1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import
UIKit
class
ViewController
:
UIViewController
{
override
func
viewDidLoad() {
super
.viewDidLoad()
let
items=[
"选项一"
,
"选项二"
UIImage
(named:
"star"
)!]
as
[
AnyObject
]
segmented=
UISegmentedControl
(items:items)
segmented.center=
self
.view.center
segmented.selectedSegmentIndex=1
//默认选中第二项
segmented.addTarget(
"segmentDidchange:"
forControlEvents:
UIControlEvents
.
ValueChanged
)
//添加值改变监听
.view.addSubview(segmented)
}
segmentDidchange(segmented:
){
//获得选项的索引
print
(segmented.selectedSegmentIndex)
//获得选择的文字
(segmented.titleForSegmentAtIndex(segmented.selectedSegmentIndex))
}
}
|