多个Pickerviews

时间:2016-10-13 08:12:43

标签: swift uipickerview

我想构建一个包含三个pickerviews的viewcontroller(它有三个相同的选项)。我在如何向标签显示一个特定行时遇到问题。我的代码:

我改变了其他部分仍然出错。编辑变量的首字母为小写。

@IBOutlet weak var picker1: UIPickerView!

@IBOutlet weak var picker2: UIPickerView!

@IBOutlet weak var picker3: UIPickerView!

var Array  = ["Shake","Swipe Up","Swipe Right"]



@IBOutlet weak var label1: UILabel!

@IBOutlet weak var label2: UILabel!

@IBOutlet weak var label3: UILabel!

var placementAnswer = 0

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    picker1.delegate = self
    picker1.dataSource = self
    picker1.tag = 1

    picker2.delegate = self
    picker2.dataSource = self
    picker2.tag = 2

    picker3.delegate = self
    picker3.dataSource = self
    picker3.tag = 3

}

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

}

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

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

    return 1
}

@IBAction func save3(_ sender: AnyObject) {
}

@IBAction func save2(_ sender: AnyObject) {
}

@IBAction func save1(_ sender: AnyObject) {


    if (placementAnswer == 0){
        label1.text = "Shake"
    }

    else if(placementAnswer == 1){
        label1.text = "Swipe Up"

    }
    else{
        label1.text = "Swipe Right"
    }


    if (placementAnswer == 0){
        label2.text = "Shake"
    }

    else if(placementAnswer == 1){
        label2.text = "Swipe Up"

    }
    else{
        label2.text = "Swipe Right"

    }
    if (placementAnswer == 0){
        label3.text = "Shake"

    }
    else if(placementAnswer == 1){
        label3.text = "Swipe Up"

    }
    else{
        label3.text = "Swipe Right"
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    picker1.delegate = self
    picker1.dataSource = self
    picker1.tag = 1

    picker2.delegate = self
    picker2.dataSource = self
    picker1.tag = 2

    picker3.delegate = self
    picker3.dataSource = self
    picker1.tag = 3

}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if pickerView.tag == 1 {
        // do things for pickerview 1
        label1.text = row
    }
    else if pickerView.tag == 2 {
        // do things for pickerview 2
        label2.text = row
    }else {
        // do for pickerview 3
    }

    placementAnswer = row
}
}

2 个答案:

答案 0 :(得分:0)

使用tag

进行区分
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    picker1.delegate = self
    picker1.dataSource = self
    picker1.tag = 1

    picker2.delegate = self
    picker2.dataSource = self
    picker1.tag = 2

    picker3.delegate = self
    picker3.dataSource = self
    picker1.tag = 3
}

所以在你的委托方法中只需检查标签

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

}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if pickerView.tag == 1 {
        // do things for pickerview 1
        Label1.text = Array[row]
    }
    else if pickerView.tag == 2 {
        // do things for pickerview 2
        Label2.text = Array[row]
    }else {
        // do for pickerview 3
    }
}

答案 1 :(得分:0)

enter image description here

在这里你是@ h44f33z。我如何让每个部分相互依赖?例如,如果我为行为名称1选择“摇动”,它将不适用于行为名称2和3.每个行为名称部分将有一个相应的操作。谢谢!