[英]Save data from selected picker view row
I have aa picker view in my view controller and want to save the data from the selected row in a variable.我的视图控制器中有一个选择器视图,并希望将所选行中的数据保存在变量中。 I don't want to have a button.我不想有一个按钮。 My Picker View is attached to the view controllers delegate and datasource.我的选择器视图附加到视图控制器委托和数据源。 Here is my picker view code:这是我的选择器视图代码:
var colors = ["ISERV 1","ISERV 2","ISERV 3"]
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return colors.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return colors[row]
}
You need to implement didSelectRow:您需要实现 didSelectRow:
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
let variable = colors[row]
}
This code you can be try这段代码你可以试试
var numbers = [1, 2 , 3]
var num:Int! = 1
func numberOfComponents(in pickerView: UIPickerView) -> Int
{
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return numbers.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
return numbers[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
num = numbers[row]
}
@IBAction func saveAccounts(_ sender: UIButton)
{
if num == 1 {
let aleart = UIAlertController(title: "The number is ONE", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "ok", style: .default) { (action) in
}
aleart.addAction(action)
present(aleart, animated: true, completion: nil)
}else if num == 2 {
let aleart = UIAlertController(title:"The number is TOW", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "ok", style: .default) { (action) in
}
aleart.addAction(action)
present(aleart, animated: true, completion: nil)
}else if num == 3 {
let aleart = UIAlertController(title: "The number is THREE", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "ok", style: .default) { (action) in
}
aleart.addAction(action)
present(aleart, animated: true, completion: nil)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.