[1个UIView中有2个选择器的2 TextField

问题描述 投票:0回答:2

我在1UIView中创建了一个带有2个选择器的2 textField,但是我无法在正确的文本字段中显示正确的选择器,因为我不知道如何识别当前视图。我用标记尝试了一些东西(但仅适用于选择器,不适用于文本字段),并创建var current = UITextField()

var genre = ["-","Homme","Femme"]
var Sport = ["-","Tennis"]
var picker = UIPickerView()
var currentTextField = UITextField()

override func viewDidLoad() {
        super.viewDidLoad()

 picker.delegate = self
    picker.dataSource = self
    GenderTextField.inputView = picker
    WitchSportTextField.inputView = picker
func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if currentTextField == GenderTextField{
        return genre.count
        }else{
            return Sport.count
        }
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if currentTextField == GenderTextField {
            GenderTextField.text = genre[row]
        }else{
            WitchSportTextField.text = Sport[row]
        }
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if currentTextField == GenderTextField{
        return genre[row]
         }else{
            return Sport[row]
        }
    }

有人提议吗?

ios swift xcode uiviewcontroller uitextfield
2个回答
0
投票

这里是完整的代码

var genre = ["-","Homme","Femme"]
var Sport = ["-","Tennis"]
var genrepicker = UIPickerView()
var Sportpicker = UIPickerView()
var genretTextField = UITextField()
var SportTextField = UITextField()

override func viewDidLoad() {
        super.viewDidLoad()

    genrepicker.delegate = self
    genrepicker.dataSource = self

    Sportpicker.delegate = self
    Sportpicker.dataSource = self
    genretTextField.inputView = genrepicker
    SportTextField.inputView = Sportpicker

func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if pickerView == genrepicker{
        return genre.count
        }else{
            return Sport.count
        }
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if pickerView == genrepicker {
            GenderTextField.text = genre[row]
        }else{
            WitchSportTextField.text = Sport[row]
        }
    }
   func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if pickerView == genrepicker{
        return genre[row]
         }else{
            return Sport[row]
        }
    }

0
投票

首先在viewDidLoad中将您的两个TextField委托都分配给self。>

GenderTextField.delegate = self
WitchSportTextField.delegate = self

使用此扩展名来标识哪个TextField

是活动的。
extension YourClass: UITextFieldDelegate {
    func textFieldDidBeginEditing(_ textField: UITextField) {
         currentTextField = textField
         picker.reloadAllComponents() //Reload for accurate data
    }
}

注意:变量名应以小写字母开头。使用描述性名称来阐明您的意图:

看此:https://github.com/MLSDev/development-standards/blob/master/platform/ios/swift-coding-conventions.md

© www.soinside.com 2019 - 2024. All rights reserved.