前端之家收集整理的这篇文章主要介绍了
Swift UIPickerView,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import UIKit
class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var label3: UILabel!
let arrData = ["北京","上海","广州"]
override func viewDidLoad() {
super.viewDidLoad()
let pickerView = UIPickerView.init()
pickerView.frame = CGRectMake(0,20,self.view.frame.size.width,200)
pickerView.delegate = self;
pickerView.dataSource = self;
self.view .addSubview(pickerView)
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return arrData.count
}
func pickerView(pickerView: UIPickerView,numberOfRowsInComponent component: Int) -> Int {
return arrData.count
}
func pickerView(pickerView: UIPickerView,titleForRow row: Int,forComponent component: Int) -> String? {
return arrData[row]
}
func pickerView(pickerView: UIPickerView,didSelectRow row: Int,inComponent component: Int) {
switch component {
case 0 : label1.text = arrData[row]
case 1 : label2.text = arrData[row]
case 2 : label3.text = arrData[row]
default: break
}
}
}
原文链接:https://www.f2er.com/swift/324067.html