我是Swift的新手,但我觉得我很好地接受它。
我有四个带有InputView的UITextField显示UIPickerViews,我以编程方式将它们放在应用程序中。我的代码显示没有错误,所有UIPickerViews弹出(picker,picker2,picker3,picker4),但只有“气候”的数组显示在所有四个视图中。所以我明显的问题是......在世界上我如何让剩下的数组(环境,区域,预算)显示在他们备受推崇的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.
}
}
答案 0 :(得分:0)
我不喜欢使用代码(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]
}
}
您的问题只是因为您没有希望从用户刚刚制作了一个部分的选择器视图中看到该标记。