所以我试着这样做,当我点击键盘上的reutrn/done键时,键盘会隐藏起来,但由于某种原因,当我尝试委派两个文本字段时,每当我点击nameTextField时,它会认为我在按jobTextField并显示pickerView。有人明白吗?
视图控制器代码

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource {`

    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var jobTextField: UITextField!

    @IBOutlet weak var jobPickerView: UIPickerView!

    @IBAction func setupDoneButton(_ sender: Any) {
        if (nameTextField.text?.isEmpty == false) {
            defaults.set(nameTextField.text, forKey: "name")
            defaults.set(jobTextField.text, forKey: "job")
        }
    }

    let defaults = UserDefaults.standard
    let jobs = ["Real Estate Agent","Retail","Teacher", "Student", "Other"]


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        jobPickerView.isHidden = true;
        jobPickerView.delegate = self
        jobPickerView.dataSource = self
        jobTextField.delegate = self

        nameTextField.delegate = self
        nameTextField.returnKeyType = UIReturnKeyType.done

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func prepare (for segue: UIStoryboardSegue, sender: Any!) {
        if (segue.identifier == "setupDoneButtonSegue") {
            let svc = segue.destination as! SecondViewController
            svc.firstDataPassed = nameTextField.text
            svc.secondDataPassed = jobTextField.text
        }
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return jobs.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return jobs[row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        jobTextField.isHidden = false;
        jobPickerView.isHidden = true;
        jobTextField.text = jobs[row];
    }

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        jobTextField.isHidden = true
        jobPickerView.isHidden = false;
        return false
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return false
    }
}

最佳答案

由于您正在为这两个字段设置UITextFieldDelegate,您需要在textFieldShouldBeginEditing中添加条件,因此它将仅对pickerView显示jobTextField

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    jobTextField.isHidden = false
    jobPickerView.isHidden = true
    if jobTextField == textField {
        jobTextField.isHidden = true
        jobPickerView.isHidden = false
        return false
    }
    return true
}

关于ios - 具有2个textField.delegate = self可以取消其中之一,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51673620/

10-12 01:14