拥有多个选取器视图
Posted
技术标签:
【中文标题】拥有多个选取器视图【英文标题】:Having multiple Picker Views 【发布时间】:2018-02-19 04:11:17 【问题描述】:我试图在一个单一的视图中快速拥有多个选择器视图。 到目前为止,我已经创建了 2 个不同的文本字段,如果我单击它们,我希望它们都有不同的选择器视图。所以让我们说,如果点击文本字段#1,它会打开数组#1,第二个是文本字段#2。
我已经查看了这些问题,但它们并没有真正回答我的问题或解决我的问题:
Multiple Picker Views on a single view
Creating Multiple Dynamic Picker Views
所以我的视图控制器看起来像这样:
how my viewController should look
这是我的一段代码:
let pickOption = ["kg","lb"] //first array for first textfield
let ageOption = ["0-5", "6-10", "11-15"] //array for 2nd textfield
let pickerView = UIPickerView()
//picker View functions
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
return pickOption.count
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
return pickOption[row]
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
weightTextField.text = pickOption[row]
override func viewDidLoad()
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
weightTextField.inputView = pickerView
weightTextField.text = pickOption[0]
如前所述,第一个文本字段打开数组“pickOption”,第二个数组打开“ageOption”
编辑:'weightTextField' 是打开第一个数组的第一个文本字段(pickOption)
【问题讨论】:
如果pickerView == xxx ;否则如果pickerView == yyy ;否则 @ElTomato 嘿,谢谢您的回答!但我不明白你的意思。你能为我解释一下吗?我是 ios 和 swift 编程新手! 我为多个文本字段提供了一个选择器视图 【参考方案1】:在UITextFieldDelegate
的帮助下,我们可以从单个PickerView
处理多个UITextField
let pickOption = ["kg","lb"] //first array for first textfield
let ageOption = ["0-5", "6-10", "11-15"] //array for 2nd textfield
let pickerView = UIPickerView()
var currentTxtFldTag : Int = 10
override func viewDidLoad()
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
weightTextField.tag = 10
ageTextField.tag = 20
weightTextField.delegate = self
ageTextField.delegate = self
weightTextField.inputView = pickerView
weightTextField.text = pickOption[0]
ageTextField.inputView = pickerView
ageTextField.text = ageOption[0]
// Do any additional setup after loading the view.
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
if currentTxtFldTag == 10
return pickOption.count
else
return ageOption.count
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
if currentTxtFldTag == 10
return pickOption[row]
else
return ageOption[row]
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
if currentTxtFldTag == 10
weightTextField.text = pickOption[row]
else
ageTextField.text = ageOption[row]
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool
if textField.tag == 10 // WEIGHT OPTION
currentTxtFldTag = 10
else // AGE OPTION
currentTxtFldTag = 20
pickerView.reloadAllComponents()
return true
输出
【讨论】:
是的!这就是我要找的!太感谢了! :) 您不需要为此添加标签。只需保留对当前 UITextField 的引用即可。 var currentTxtFldTag = UITextField ??像这样??【参考方案2】:假设您有两个pickerView 控件:picker0 和picker1。
class HomeViewController: BasicViewController, UITextFieldDelegate, UITextViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource
let numberArray = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180]
var areaArray = [String]()
@IBOutlet weak var picker0: UIPickerView!
@IBOutlet weak var picker1: UIPickerView!
override func viewDidLoad()
super.viewDidLoad()
// area //
for i in 0..<11
let str = "Area "
let localStr = NSLocalizedString(str + String(i), comment: "")
areaArray.append(localStr)
// MARK: - Campaign duration & area
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
if pickerView == picker0
return numberArray.count
else
return areaArray.count
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
if pickerView == picker0
return String(numberArray[row]) + " pieces"
else
return areaArray[row]
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
if pickerView == seventhPicker
else
【讨论】:
啊,我知道你在做什么!我正在尝试解决问题!非常感谢【参考方案3】:override func viewDidLoad()
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
weightTextField.inputView = pickerView
let pickOption = ["kg","lb"] //first array for first textfield
let ageOption = ["0-5", "6-10", "11-15"] //array for 2nd textfield
let pickerView = UIPickerView()
//picker View functions
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 1
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
if weightTextField1.tag == 1
return pickOption.count
else if weightTextField2.tag == itemPicker
return ageOption.count
return 0
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
if weightTextField1.tag == 1
return pickOption[row]
else if weightTextField2.tag == 1
return ageOption[row]
return 0
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
if weightTextField1.tag == 1
weightTextField1.text = pickOption[row]
else if weightTextField2.tag == 1
weightTextField2.text = ageOption[row]
////set delegate
func textFieldDidBeginEditing(textField: UITextField!) //delegate method
if textField == weightTextField1
weightTextField1.atg = 1;
weightTextField2.atg = 0;
else if textField == weightTextField2
weightTextField1.atg = 0;
weightTextField2.atg = 1;
picker.reloadAllComponents()
【讨论】:
@VDuc 用文本字段代表试试这个以上是关于拥有多个选取器视图的主要内容,如果未能解决你的问题,请参考以下文章