迅速4多个选择器视图不起作用。我有三个数组,但只有“ strBlood”显示

时间:2019-03-13 10:52:18

标签: ios swift uipickerview

切换4个多重选择器视图不起作用。我有三个数组,但是只有“ strBlood”显示的是准确的数组,其他的strcountry和strgender不在选择器视图中显示,当我单击第三个标签代码时,崩溃使我收到信号sigbart的错误 < / p>

多个选择器视图未在swift 4的选择器视图中显示数据,我已经阅读了许多教程,但没有人解决我的问题,请检查并回答我。
当我实现3个选择器视图时,数据仅显示第一个选择器视图,即(strblood),而不显示其他数组

下面的代码运行正常,但是在按一下其他标签(“ lblblood”。“ lblcountry”)时,在选择器视图中显示该数组存在一个错误

import UIKit
import Foundation

class RegisterViewController: UIViewController  {

    var strBlood = ["O+","O-","O","A","B+"]
    var  strcountry = ["India","Canada","USA"]
    var strgender = ["Male","Female"]

    var selectedBlood: String?
    var selectedCountry: String?
    var selectedGender: String?

    @IBOutlet weak var txtGender: UITextField!
    @IBOutlet weak var txtCountry: UITextField!
    @IBOutlet weak var lblBloodGroup: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()
             self.chooseCountry()
             self.chooseBlood()
             self.choosGender()
    }

    func chooseBlood(){
        let bloodPicker = UIPickerView()
        bloodPicker.delegate = self
        self.lblBloodGroup.inputView = bloodPicker
    }

    func chooseCountry(){

        let countryname = UIPickerView()
        countryname.delegate = self
        self.txtCountry.inputView = countryname
    }
    func choosGender() {

    let gender1 = UIPickerView()
    gender1.delegate = self
    self.txtGender.inputView = gender1
    }

}
extension RegisterViewController : UIPickerViewDelegate , UIPickerViewDataSource {

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        if lblBloodGroup.isEnabled {
            print("BLOOD SELECTED")
           return strBlood.count

        }
        else if txtCountry.isEnabled{
            print("COUNTRY SELECTED")

            return strcountry.count
        }else {
             print("GENDER SELECTED")
            return strgender.count
        }

        }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        if lblBloodGroup.isEnabled{
            print("BLOOD SELECTED1")
            return strBlood[row]


        }else if txtCountry.isEnabled{

            return strcountry[row]
        }else {
            return strgender[row]
        }

       }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)  {
        if lblBloodGroup.isEnabled{
            selectedBlood = strBlood[row]
            lblBloodGroup.text = selectedBlood

        }else if txtCountry.isEnabled{
            selectedCountry = strcountry[row]
            txtCountry.text = selectedCountry
        }else {
            selectedGender = strgender[row]
            txtGender.text = selectedGender
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

要达到此目的,isEnabled并没有帮助。您需要使用isEditing,如果用户单击任何UITextField,则您的代码为:

import UIKit
import Foundation

class ViewController: UIViewController  {

    var strBlood = ["O+","O-","O","A","B+"]
    var strcountry = ["India","Canada","USA"]
    var strgender = ["Male","Female"]

    var selectedBlood: String?
    var selectedCountry: String?
    var selectedGender: String?

    @IBOutlet weak var txtGender: UITextField!
    @IBOutlet weak var txtCountry: UITextField!
    @IBOutlet weak var lblBloodGroup: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()
        self.chooseCountry()
        self.chooseBlood()
        self.choosGender()
    }

    func chooseBlood(){
        let bloodPicker = UIPickerView()
        bloodPicker.delegate = self
        self.lblBloodGroup.inputView = bloodPicker
    }

    func chooseCountry(){

        let countryname = UIPickerView()
        countryname.delegate = self
        self.txtCountry.inputView = countryname
    }
    func choosGender() {

        let gender1 = UIPickerView()
        gender1.delegate = self
        self.txtGender.inputView = gender1
    }
}
extension ViewController : UIPickerViewDelegate , UIPickerViewDataSource {

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        if lblBloodGroup.isEditing {
            print("BLOOD SELECTED")
            return strBlood.count

        }
        else if txtCountry.isEditing{
            print("COUNTRY SELECTED")

            return strcountry.count
        }else {
            print("GENDER SELECTED")
            return strgender.count
        }

    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        if lblBloodGroup.isEditing{
            print("BLOOD SELECTED1")
            return strBlood[row]


        }else if txtCountry.isEditing{

            return strcountry[row]
        }else {
            return strgender[row]
        }

    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)  {
        if lblBloodGroup.isEditing{
            selectedBlood = strBlood[row]
            lblBloodGroup.text = selectedBlood

        }else if txtCountry.isEditing{
            selectedCountry = strcountry[row]
            txtCountry.text = selectedCountry
        }else {
            selectedGender = strgender[row]
            txtGender.text = selectedGender
        }
    }
}

结果将是:

enter image description here