如何在swift的选择器中获取所选项目的标题和项目的价值?

时间:2018-05-15 11:57:10

标签: swift uipickerview uipickerviewdatasource uipickerviewdelegate

首先让我描述视图控制器(页面设计)方面。我添加了一个选择器视图,一个标签和一个按钮,这是我的viewController文件

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    //var myArray2 = [["Alex", "de Souza", "1977"],["Ariel", "Ortega", "1974"],["Lefter", "KucukAndonyadis", "1925"]]
    //structure of myArray2: name, lastname, bday

    var myArray = ["Alex", "de Souza", "1977"]

    var indexValueOfSelectedItem = 0
    var nameValueOfSelectedItem = ""
    var lastnameValueOfSelectedItem = ""
    var bdateOfSelectedItem = 0

    @IBOutlet weak var lblResult: UILabel!
    @IBOutlet weak var pickerStudent: UIPickerView!
    override func viewDidLoad() {
        super.viewDidLoad()
        pickerStudent.delegate = self
        pickerStudent.dataSource = self
    }

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

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

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

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        indexValueOfSelectedItem = row
        //nameValueOfSelectedItem = row
        //lastnameValueOfSelectedItem = row
        //bdateOfSelectedItem = row
    }

    @IBAction func buttonClick(_ sender: Any) {
        lblResult.text = String (indexValueOfSelectedItem)
    }
}

如果我使用myArray2而不是myArray,我如何获得所选项目详细信息的名称,如姓名,姓氏等?我想在标签中显示所选项目的名称,姓氏和日期。

3 个答案:

答案 0 :(得分:1)

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

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

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

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        indexValueOfSelectedItem = row

        print("name: \(myArray2[indexValueOfSelectedItem][0])")
        print("last name: \(myArray2[indexValueOfSelectedItem][1])")
        print("birthdate: \(myArray2[indexValueOfSelectedItem][2])")
    }

答案 1 :(得分:0)

var pickerDataSource = [["Alex", "de Souza", "1977"],["Ariel", "Ortega", "1974"],["Lefter", "KucukAndonyadis", "1925"]]


var selectedModel = ["", "", ""]


@IBOutlet weak var lblResult: UILabel!
@IBOutlet weak var pickerStudent: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
pickerStudent.delegate = self
pickerStudent.dataSource = self
}

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

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

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

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    selectedModel = pickerDataSource[row]
}

@IBAction func buttonClick(_ sender: Any) {
    print(selectedModel)
}

答案 2 :(得分:0)

你可以通过

获得价值
@IBAction func buttonClick(_ sender: Any) {
lblResult.text =  myArray[pickerStudent.selectedRow(inComponent: 0)]} }

并且要获取所选索引,您可以使用

pickerStudent.selectedRow(inComponent: 0)

使用委托方法,您可以在用户滚动选择器视图时获取值,但如果用户不滚动并想要选择第一个数据,那么您可以使用此方法,而且您也不需要任何委托方法