gpt4 book ai didi

ios - if语句中根据UIPickerView选中的行更改标签

转载 作者:行者123 更新时间:2023-11-28 14:47:30 24 4
gpt4 key购买 nike

我仔细搜索了多个页面和帖子,但无法完全找到我需要的东西。我目前正在处理一个项目,其中一个 Controller 有多个文本字段,使用单个 UIPickerView 输出,有时标签需要根据用户的选择进行更改。

如何根据 UIPickerView 的选定行更改标签,尤其是在 if 语句中?

我做了一个简单的测试代码和例子来演示。

代码

class TestViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var categoryOutput: UITextField!
@IBOutlet weak var shapeOutput: UITextField!
@IBOutlet weak var lengthDiaOutput: UITextField!
@IBOutlet weak var heightOutput: UITextField!

@IBOutlet weak var dimensionLabel: UILabel!

let category = ["Perimeter", "Area"]
let shape = ["Rectangle", "Circle", "Triangle"]


// MARK: - viewDidLoad()

override func viewDidLoad() {
super.viewDidLoad()

createCategoryPicker()
createShapePicker()
}


// MARK: - UIPickerViews

// Create a UIPickerView as an input text field for 'Category'.

func createCategoryPicker() {

let categoryPicker = UIPickerView()
categoryPicker.delegate = self
categoryPicker.tag = 1

categoryOutput.inputView = categoryPicker

categoryPicker.backgroundColor = UIColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 1.0)
}

// Create a UIPickerView as an input text field for 'Shape'.

func createShapePicker() {
let shapePicker = UIPickerView()
shapePicker.delegate = self
shapePicker.tag = 2

shapeOutput.inputView = shapePicker

shapePicker.backgroundColor = UIColor(red: 238/255.0, green: 238/255.0, blue: 238/255.0, alpha: 1.0)
}

}


// MARK: - Extensions

// Create a UIPickerView.

extension TestViewController: UIPickerViewDelegate, UIPickerViewDataSource {

// Set number of lists.

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

return 1
}

// Set number of rows in list.

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

if pickerView.tag == 1 {
return category.count }
if pickerView.tag == 2 {
return shape.count }
return 0
}

// Set content of list.

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

if pickerView.tag == 1 {
return category[row] }
if pickerView.tag == 2 {
return shape[row] }
return nil
}

// Grab selection and change label.

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

if pickerView.tag == 1 {
categoryOutput.text = category[row] }
if pickerView.tag == 2 {
shapeOutput.text = shape[row]

if component == 1 {
dimensionLabel.text = "Diameter" }
else {
dimensionLabel.text = "Length" }
}
}

// Set appearance of contents.

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

var label: UILabel

if let view = view as? UILabel {
label = view }
else { label = UILabel() }

if pickerView.tag == 1 {
label.text = category[row] }
if pickerView.tag == 2 {
label.text = shape[row] }

label.font = UIFont(name: "Avenir", size: 17)
label.textColor = .black
label.textAlignment = .center

return label
}
}

这是需要关注的部分。

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

if pickerView.tag == 1 {
categoryOutput.text = category[row] }
if pickerView.tag == 2 {
shapeOutput.text = shape[row]

if component == 1 {
dimensionLabel.text = "Diameter" }
else {
dimensionLabel.text = "Length" }
}
}

例子

enter image description here

文本标签DimensionCircle时不会更改为Diameter> 被选中。

最佳答案

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

if pickerView.tag == 1 {
categoryOutput.text = category[row] }
if pickerView.tag == 2 {
shapeOutput.text = shape[row]

if row == 1 //Circle{
dimensionLabel.text = "Diameter" }
else {
dimensionLabel.text = "Length" }
}
}

关于ios - if语句中根据UIPickerView选中的行更改标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50127663/

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