gpt4 book ai didi

swift - 我怎样才能在选择器中快速到达所选项目的标题和项目的值(value)?

转载 作者:行者123 更新时间:2023-11-28 14:44:55 27 4
gpt4 key购买 nike

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

最佳答案

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])")
}

关于swift - 我怎样才能在选择器中快速到达所选项目的标题和项目的值(value)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50349589/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com