一个ViewController中有两个UIPickerViews

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

两个PickerViews selectTypeOfWorkChoicesselectLocationChoices显示不正确。

一个函数dismissPickerView()似乎运行良好。但是,另一个函数“ createPickerView()”有一些问题。尽管出现了UIpickerviews,但我看不到UIPickerViews中的选择,也不知道为什么。

有人可以帮我找出我的代码有什么问题吗?

@IBOutlet weak var selectTypeOfWorkChoices: UIPickerView!

@IBOutlet weak var selectLocationChoices: UIPickerView!

override func viewDidLoad() {
    super.viewDidLoad()

    createPickerView()
    dismissPickerView()

    }

var typeOfWork = ["--", "a", "b", "c"]

var location = ["--", "A", "B", "C"]

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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    var countrows : Int = typeOfWork.count
    if pickerView == selectLocationChoices {
        countrows = self.location.count
    }
    return countrows
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView == selectTypeOfWorkChoices {
        let titleRow = typeOfWork[row]

        return titleRow
    }
    else if pickerView == selectLocationChoices {
        let titleRow = location[row]

        return titleRow
    }
    return ""
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if pickerView == selectTypeOfWorkChoices {
        selectedPriority = typeOfWork[row]
        selectTypeOfWork.text = selectedPriority

        self.selectTypeOfWork.text = self.typeOfWork[row]

    }
    else if pickerView == selectLocationChoices {
        locationSelectedPriority = location[row]
        selectLocation.text = locationSelectedPriority

        self.selectLocation.text = self.location[row]

    }


}

var selectedPriority :  String?
var locationSelectedPriority :  String?


func createPickerView() {

    let pickerView = UIPickerView()
    pickerView.delegate = self

    self.selectTypeOfWorkChoices.delegate = self
    self.selectTypeOfWorkChoices.dataSource = self
    self.selectLocationChoices.delegate = self
    self.selectLocationChoices.dataSource = self


        selectTypeOfWork.inputView = selectTypeOfWorkChoices

        selectLocation.inputView = selectLocationChoices



}


@objc func dismissPickerView() {
    let toolBar = UIToolbar()
    toolBar.sizeToFit()

    let doneButton = UIBarButtonItem(title:"Done", style: .plain, target: self, action: #selector(self.dismissKeyboard))
    toolBar.setItems([doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true

    selectTypeOfWork.inputAccessoryView = toolBar
    selectLocation.inputAccessoryView = toolBar
}

@objc func dismissKeyboard () {
    view.endEditing(true)
}
swift xcode uipickerview
1个回答
0
投票

将输入视图分配给您的文本字段后,重新加载选择器视图。

func createPickerView() {

    let pickerView = UIPickerView()
    pickerView.delegate = self

    self.selectTypeOfWorkChoices.delegate = self
    self.selectTypeOfWorkChoices.dataSource = self
    self.selectLocationChoices.delegate = self
    self.selectLocationChoices.dataSource = self

    selectTypeOfWork.inputView = selectTypeOfWorkChoices
    selectLocation.inputView = selectLocationChoices

    //Reload Pickerview
    self.selectTypeOfWorkChoices.reloadAllComponents()
    self.selectLocationChoices.reloadAllComponents()
}
© www.soinside.com 2019 - 2024. All rights reserved.