我有一个类,其中我对多个textField使用单个pickerView,效果很好。
我还设置了条件if the array that is being used for pickerView's dataSource is empty, fire a function to show an alert
。糟糕的是,numberOfRowsInComponent
运行三次,而警报功能也运行三次。我不知道为什么会这样。
以下是代码段:
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
...
case "addExpense"?:
if (activeTextField[0].isFirstResponder) {
if aRR_expenseCategory.count == 0 {
ifArrayIsEmpty(forTextField: "category")
return 0
} else {
return aRR_expenseCategory.count
}
} else if (activeTextField[1].isFirstResponder) {
if aRR_expenseSubCategory.count == 0 {
ifArrayIsEmpty(forTextField: "subCategory")
return 0
} else {
return aRR_expenseSubCategory.count
}
} else if (activeTextField[4].isFirstResponder) {
return _aRR_debitedFrom.count
} else {
return 0
}
...
}
fileprivate func ifArrayIsEmpty(forTextField tfString: String) {
// Show Alert
}
答案 0 :(得分:0)
您可以在UITextField的委托方法中执行此操作,即didBeginEditing。
在didBeginEditing内部,重新加载pickerView并检查数据源并显示警报。