让我先描述视图控制器(页面设计)方面。我添加了一个选择器视图、一个标签和一个按钮,这是我的 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,我怎样才能获得所选项目详细信息的名称,如姓名、姓氏等?我想在标签中显示所选项目的姓名、姓氏和生日。