import
UIKit
class
ViewController
:
UIViewController
,
UIPickerViewDelegate
,
UIPickerViewDataSource
{
var
pickerView:
UIPickerView
!
override
func
viewDidLoad() {
super
.viewDidLoad()
pickerView=
UIPickerView
()
pickerView.dataSource=
self
pickerView.delegate=
self
pickerView.selectRow(1,inComponent:0,animated:
true
)
pickerView.selectRow(2,inComponent:1,animated:
true
)
pickerView.selectRow(3,inComponent:2,animated:
true
)
self
.view.addSubview(pickerView)
var
button=
UIButton
(frame:
CGRectMake
(0,0,100,30))
button.center=
self
.view.center
button.backgroundColor=
UIColor
.blueColor()
button.setTitle(
"获取信息"
,forState:.
Normal
)
button.addTarget(
self
, action:
"getPickerViewValue"
,
forControlEvents:
UIControlEvents
.
TouchUpInside
)
self
.view.addSubview(button)
}
func
numberOfComponentsInPickerView( pickerView:
UIPickerView
) ->
Int
{
return
3
}
func
pickerView(pickerView:
UIPickerView
,numberOfRowsInComponent component:
Int
) ->
Int
{
return
9
}
func
pickerView(pickerView:
UIPickerView
!,titleForRow row:
Int
,forComponent component:
Int
)
->
String
!{
return
String
(row)+
"-"
+
String
(component)
}
func
getPickerViewValue(){
var
alertView=
UIAlertView
();
alertView.title=
"被选中的索引为"
alertView.message=
String
(pickerView.selectedRowInComponent(0))+
"-"
+
String
(pickerView!.selectedRowInComponent(1))+
"-"
+
String
(pickerView.selectedRowInComponent(2))
alertView.addButtonWithTitle(
"OK"
)
alertView.show()
}
}
请发表评论