gpt4 book ai didi

ios - UIPickers 数据不会从 UITextField 显示

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

我是 Swift 新手,但我觉得我学得很好。

我有四个带有显示 UIPickerViews 的 InputView 的 UITextField,并且我以编程方式将它们放置在应用程序中。我的代码没有显示错误,所有 UIPickerViews 都会弹出(picker、picker2、picker3、picker4),但只有“climate”中的数组显示在所有四个 View 中。所以我明显的问题是......我到底如何让剩余的数组(环境、区域、预算)显示在他们受人尊敬的 UIPickerViews 中!

另外,侧面请求,我如何禁用文本字段,以便没有人能够将任何内容粘贴到文本字段中

这是我的代码:

import UIKit

class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {

@IBOutlet weak var climateText: UITextField!
@IBOutlet weak var environmentText: UITextField!
@IBOutlet weak var regionText: UITextField!
@IBOutlet weak var budgetText: UITextField!

var picker = UIPickerView()
var picker2 = UIPickerView()
var picker3 = UIPickerView()
var picker4 = UIPickerView()


//MARK: Arrays for PickerViews
var climates = ["Hot","Warm","Cold","Anywhere"]
var environment = ["Beach","City","Mountain","Rural","Anywhere"]
var region = ["Central America","Carribean","Europe","North America","South America","Anywhere"]
var budget = ["$0-500","$501-1000","$1001-1500","$1501-2500","$2501 and up"]


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

picker.tag = 0
picker2.tag = 1
picker3.tag = 2
picker4.tag = 3

picker.delegate = self
picker2.delegate = self
picker3.delegate = self
picker4.delegate = self

picker.dataSource = self
picker2.dataSource = self
picker3.dataSource = self
picker4.dataSource = self

climateText.inputView = picker
environmentText.inputView = picker2
regionText.inputView = picker3
budgetText.inputView = picker4
}


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


func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if picker.tag == 0 {
return climates.count
} else if picker2.tag == 1 {
return environment.count
} else if picker3.tag == 2 {
return region.count
} else if picker4.tag == 3 {
return budget.count
}
return 1
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if picker.tag == 0 {
return climates[row]
} else if picker2.tag == 1 {
return environment[row]
} else if picker3.tag == 2 {
return region[row]
} else if picker4.tag == 3 {
return budget[row]
}
return ""
}


func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if picker.tag == 0 {
climateText.text = climates[row]
} else if picker2.tag == 1 {
environmentText.text = environment[row]
} else if picker3.tag == 2 {
regionText.text = region[row]
} else if picker4.tag == 3 {
budgetText.text = budget[row]
}
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

最佳答案

我不喜欢使用标签 ( see point 2 here ),但为了解决这个问题,请更改 didSelectRow 函数来执行此操作:

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 0 {
climateText.text = climates[row]
} else if pickerView.tag == 1 {
environmentText.text = environment[row]
} else if pickerView.tag == 2 {
regionText.text = region[row]
} else if pickerView.tag == 3 {
budgetText.text = budget[row]
}
}

您的问题只是在于您不想从用户刚刚创建的部分的选择器 View 中看到标签。

关于ios - UIPickers 数据不会从 UITextField 显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36144929/

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