我开发了一个应用程序,其中我使用了两个带有tag属性的pickerview框:一个用于性别,一个用于项目列表。选择任意一个pickerview后,它将以某种方式将另一个pickerview增加到具有实际值的条目,而不是从我在[0]中创建的空条目开始。我进行了第一个选择,当我进入第二个列表时,它已经坐在50-55上(第一个具有值的条目位于1。如果我不从1移到另一个,然后回到1标签仍然是空白。如果我单击预选项目后再返回,一切正常,请粘贴下面的pickerview代码-让我知道是否需要更多信息,谢谢前进!</ p>
Added a video as to what exactly is happening:
let gendOption = ["", "Male","Female"] // first array for first textfield
let actOption = ["", "50%-55%", "55%-60%", "60%-65%", "65%-70%", "70%-75%", "75%-80%", "80%-85%", "85%-90%", "90%-95%"] // array for 2nd textfield
let pickerView = UIPickerView()
var currentTxtFldTag : Int = 10
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
// return priorityTypes.count
if currentTxtFldTag == 10
{
return gendOption.count
}
else
{
return actOption.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
// return priorityTypes[row]
if currentTxtFldTag == 10
{
return gendOption[row]
}
else
{
return actOption[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//selectedPriority = priorityTypes[row]
//activityTextBox.text = selectedPriority
if currentTxtFldTag == 10
{
genderSelection.text = gendOption[row]
}
else
{
activityTextBox.text = actOption[row]
}
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField.tag == 10 // *** OPTION
{
currentTxtFldTag = 10
}
else // activity OPTION
{
currentTxtFldTag = 20
}
pickerView.reloadAllComponents()
return true
}
func createPickerView() {
let pickerView = UIPickerView()
pickerView.delegate = self
activityTextBox.inputView = pickerView
}`
答案 0 :(得分:0)
与其使用此currentTxtFldTag
标志来查看您所指的pickerView
,只需将每个pickerView
保存为变量并检查您所指的pickerView
直接在您的每个委托方法中使用,例如:
lazy var genderPickerView: UIPickerView = {
let picker = UIPickerView()
picker.delegate = self
return picker
}()
lazy var otherPickerView: UIPickerView = {
let picker = UIPickerView()
picker.delegate = self
return picker
}()
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// Use this check in each of your delegate methods for UIPickerViewDelegate
if pickerView == genderPickerView
{
genderSelection.text = gendOption[row]
}
else
{
activityTextBox.text = actOption[row]
}
}