这是我的代码。我会说我已经采取的步骤,但是它们只是没有意义,而且我会很开心,我对Swift和Xcode还是很陌生。但是,如何声明一组组件?我以为我已经足够地隔开它们了,我已经诚实地搜索了指南,但是没有人真正专注于下拉列表和下拉列表中的变量。

import UIKit

class SecondViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource  {

    @IBOutlet weak var backBTN: UIButton!
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var labelTwo: UILabel!
    @IBOutlet weak var pickerView: UIPickerView!
    @IBOutlet weak var pickerviewTwo: UIPickerView!

    var pickerData = ["X", "Y"]
    var pickerdataTwo = ["R","D","C"]

    override func viewDidLoad() {
        super.viewDidLoad()
        pickerView.delegate = self
        pickerView.dataSource = self
        pickerviewTwo.delegate = self
        pickerviewTwo.dataSource = self

    }

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

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

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

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

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        label.text = pickerData[row]
    }

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

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

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

    func pickerviewTwo(_ pickerviewTwo: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        labelTwo.text = pickerdataTwo[row]
    }
}

最佳答案

您的选择器视图代码错误。首先,我建议将它们的名称从pickerViewpickerViewTwo更改为更具描述性的名称。

选择器视图所需的功能不能多次使用,因此会出现错误。正确的方法是:

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

//After renaming the first pickerView to topPickerView
if pickerView == topPickerView {
    return pickerData[row]
}
else {
    return pickerDataTwo[row]
}
}


每个必需的函数中都应该有一个if-else,if语句表示if pickerView == oneOfThePickerViewsName,if语句。发生的事情是在设置选择器时,它检查它是哪个选择器,并为此使用代码。
其余的格式如下:

//pickerView has been renamed to topPickerView
func numberOfComponents(in pickerView: UIPickerView) -> Int {
    //You wouldn't need to do this here as its the same for both of them.
    //
    if pickerView == topPickerView
    {
        return 1
    }
    else
    {
        return 1
    }
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if pickerView == topPickerView
    {
        return pickerData.count
    }
    else
    {
        return pickerdataTwo.count
        // should actually be formatted pickerDataTwo because of camelCase.
    }
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent
    component: Int) -> String? {
    if pickerView == topPickerView
    {
        return pickerData[row]
    }
    else
    {
        return pickerdataTwo[row]
        // should actually be formatted pickerDataTwo because of camelCase.
    }
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent
    component: Int) {
    if pickerView == topPickerView
    {
        label.text = pickerData[row]
    }
    else
    {
         labelTwo.text = pickerdataTwo[row]
        // should actually be formatted pickerDataTwo because of camelCase.
    }
}

10-07 19:37
查看更多