Swift iOS: (Примечание: пожалуйста, измените имя переменной по требованию)
Шаг 1: объявите переменные и объекты
var objPickerView : UIPickerView = UIPickerView()
// select the picker view data values
objPickerView.delegate=self;
objPickerView.dataSource=self;
objPickerView.showsSelectionIndicator=true;
// show picker view when text field clicked
self.smokingStatusText!.inputView = objPickerView
// reload the component of picker
self.objPickerView.reloadAllComponents()
// select the value of picker status(Which row must be selected)
self.objPickerView.selectRow(sStatus!, inComponent: PickerComponent.size.rawValue, animated: false)
Шаг 2: объявите делегатов и протокол:
// programming mark ----- ---- ----- ----- ---- ----- ----- ---- -----
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
// programming mark ----- ---- ----- ----- ---- ----- ----- ---- -----
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
//MARK: UIPickerView Delegates
// programming mark ----- ---- ----- ----- ---- ----- ----- ---- -----
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
smokingStatusText!.text = pickerData[row]
self.smokerStatusNum = String(row)
}
// programming mark ----- ---- ----- ----- ---- ----- ----- ---- -----
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let titleData = pickerData[row]
let font:UIFont? = UIFont(name: "Georgia", size: 14.0)
let attrString = NSAttributedString(
string: titleData,
attributes: NSDictionary(
object: font!,
forKey: NSFontAttributeName))
return attrString
}
// programming mark ----- ---- ----- ----- ---- ----- ----- ---- -----
//size the components of the UIPickerView
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 36.0
}
func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
return 300
}