gpt4 book ai didi

Swift - 返回存储在结构中的元素

转载 作者:行者123 更新时间:2023-11-30 11:46:52 25 4
gpt4 key购买 nike

如何检索存储在结构体数组中的元素?我想在加载 View 时显示艾伯塔省行。

查看时将出现:

selectedProvince = client?.province 我从服务器/数据库获取 AB。我想在 pickerview 上显示艾伯塔省。目前显示的是AB。

点击文本字段时会显示选取器 View 。因此,AB 是该文本字段的值。

我使用选择器 View 来显示名称,选择后我将缩写存储在变量中。之后,数据被保存。

当我加载 View 时,我加载数据。省份的存储方式如 AB 所示。

struct Province {
let name, abbrev : String
}



let provinces = [Province(name: "Alberta", abbrev: "AB"), Province(name: "British Columbia", abbrev: "BC"),
Province(name: "Manitoba", abbrev: "MB"), Province(name: "New Brunswick", abbrev: "NB"),
Province(name: "Newfoundland and Labrador", abbrev: "NL"), Province(name: "Northwest Territorie", abbrev: "NT"),
Province(name: "Nova Scotia", abbrev: "NS"), Province(name: "Nunavut", abbrev: "NU"),
Province(name: "Ontario", abbrev: "ON"), Province(name: "Prince Edward Island", abbrev: "PE"),
Province(name: "Quebec", abbrev: "QC"), Province(name: "Saskatchewan", abbrev: "SK"),
Province(name: "Yukon", abbrev: "YT")]


var client: Client?
var selectedProvince: String?

override func viewWillAppear(_ animated: Bool) {
self.title = "New"
if (client?.client_id) != nil {
self.title = "Edit"
nameTextField.text = client?.name
provinceTextField.text = client?.province
cityTextField.text = client?.city
addressTextField.text = client?.address
postalCodeTextField.text = client?.postal_code
selectedProvince = client?.province
}
}



override func viewDidLoad() {
super.viewDidLoad()
tableView.sectionHeaderHeight = 50.0;

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Save", style: .plain, target: self, action: #selector(save))


let thePicker = UIPickerView()
provinceTextField.inputView = thePicker
thePicker.delegate = self

// ToolBar
let toolBar = UIToolbar()
toolBar.barStyle = .default
toolBar.isTranslucent = true
toolBar.tintColor = UIColor(red: 92/255, green: 216/255, blue: 255/255, alpha: 1)
toolBar.sizeToFit()

// Adding Button ToolBar
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ClientDetailViewController.doneClick))
let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(ClientDetailViewController.cancelClick))
toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
provinceTextField.inputAccessoryView = toolBar

}

@objc func doneClick() {
provinceTextField.resignFirstResponder()
}
@objc func cancelClick() {
provinceTextField.resignFirstResponder()
}

// MARK: - Picker view
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}

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

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

func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
provinceTextField.text = provinces[row].name
selectedProvince = provinces[row].abbrev
}

最佳答案

首先,您需要对选择器 View 的强引用,这是顶层的属性:

var thePicker = UIPickerView()

并在viewDidLoad中设置参数

provinceTextField.inputView = thePicker
thePicker.delegate = self

viewWillAppear 中检查 selectedProvince 是否不 nil 并(安全地)获取 Province 实例的索引。然后设置对应的行

selectedProvince = client?.province
if let province = selectedProvince,
let index = provinces.index(where: { $0.abbrev == province }) {
thePicker.selectRow(index, inComponent: 0, animated: false)
}

关于Swift - 返回存储在结构中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48734115/

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