为什么我的 pickerview 没有出现在某些文本字段中?

Why my pickerview doesn't appear on certain text field?

我发现 this video 哪里显示了将带有字段的选择器视图添加到应用程序的方法。我添加了三个字段:

@IBOutlet weak var position: UITextField!
@IBOutlet weak var emplFilter: UITextField!
@IBOutlet weak var locFilter: UITextField!

还有表示当前文本字段和选择器的变量:

var currentTextField = UITextField()
var currentPicker = UIPickerView()

并且还添加了实现:

UITableViewDelegate, UITableViewDataSource,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource

添加了所有方法:

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if currentTextField == locFilter{
            return location.count
        }else if currentTextField == test{
            return proffestion.count
        }else if currentTextField == emplFilter{
            return employer.count
        }else{
            return 0
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if currentTextField == locFilter{
            return location[row].name
        }else if currentTextField == test{
            return proffestion[row].name
        }else if currentTextField == emplFilter{
            return employer[row].name
        }else{
            return ""
        }
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if currentTextField == locFilter{
            
            locFilter.text = location[row].name
            filters!["location"] = location[row].id
            self.view.endEditing(true)
            
        }else if currentTextField == test{
            
            filters!["prof"] = proffestion[row].id
            test.text =  proffestion[row].name
            self.view.endEditing(true)
            
        }else if currentTextField == emplFilter{
            
            emplFilter.text = employer[row].name
            filters!["employer"] = "\(String(describing: employer[row].id))"
            self.view.endEditing(true)
        }
    }
    
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        self.currentPicker.delegate = self
        self.currentPicker.dataSource = self
        
        print("click")
        
        
        currentTextField = textField
        
        
        if currentTextField == locFilter{
            currentTextField.inputView = currentPicker
        }else if currentTextField == test{
           currentTextField.inputView = currentPicker
        }else if currentTextField == emplFilter{
           currentTextField.inputView = currentPicker
        }
        
        self.currentPicker.delegate = self
        self.currentPicker.dataSource = self
    }

这里我遇到了一个问题 - 当我点击 position 字段时,我没有在日志中看到任何输出。我不明白为什么会这样,我尝试添加另一个字段,但并没有改变这种情况。我尝试使用另一个数组,但我也没有看到任何选择器视图。我试图在互联网上寻找任何可能的解决方案,但我没有成功:(

设置 position UITextFielddelegate 应该可以解决问题。

position.delegate = self

我还想指出,您不需要在每次文本字段开始编辑时重复设置 delegatedataSource of UIPickerView