我是swift Xcode的新手,想在UIPickerView上获得一些帮助。
我正在尝试创建两个UIPickerViews,一个名为“From”,一个名为“To”。 “From”包含A,B和“To”包含B,C
基本上,如果“From”是A而“To”是B,则汽车的照片将出现在同一个viewController上。 如果“From”是B而“To”是B则会出现草莓照片,依此类推。
任何人都可以给我任何关于如何做到这一点的提示吗?我只设法创建了第一个UIPickerView。
import UIKit
class ViewController: UIViewController,UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet var Picker1:UIPickerView!
var Array = ["A","B","C"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib
Picker1.delegate = self
Picker1.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return Array[row]
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return Array.count
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
}
答案 0 :(得分:2)
var savedVal1 = "" as String
var savedVal2 = "" as String
var array1 = NSArray()
var array2 = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
Picker1.delegate = self
Picker1.dataSource = self
Picker1.tag = 1
Picker2.delegate = self
Picker2.dataSource = self
Picker2.tag = 2
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 1 {
return array1[row]
}
else{
return array2[row]
}
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 1 {
return array1.count
}
else{
return array2.count
}
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int){
if pickerView.tag == 1 {
savedVal1 = array1[row]
// compare 2 values here & then take steps which fulfill your criteria.
}
else{
savedVal2 = array2[row]
//compare 2 values here & then take steps which fulfill your criteria.
}
}
希望它有所帮助。
答案 1 :(得分:0)
你的PickerView2在哪里?
@IBOutlet var Picker2:UIPickerView!
在viewdidload加载中完成
Picker1.delegate = self
Picker1.dataSource = self
如果您使用相同的数据源或不同,请检查UIPickerViewDelegate中的选择器视图,UIPickerViewDataSource方法,如
If (Picker1) {
} else if (Picker2) {
}