Swift 2.1 - UIPickerView 不能正常工作
Swift 2.1 - UIPickerView doesn't work well
我用的是最新版的Xcode,也是最新版的Swift。我按照此 link 的步骤进行操作,但 pickerview 没有出现在模拟器中。代码如下。有人可以帮我检查一下吗?非常感谢!
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate {
@IBOutlet var imageView: UIImageView!
@IBOutlet var infoLabel: UILabel!
@IBOutlet var picker: UIPickerView!
//var fruits: [String] = [String]()
var fruits = ["Pick a fruit" , "Apples" , "Oranges" , "Lemons" , "Blueberries"]
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = UIImage(named: "fruit.jpg")
// picker.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickeView: UIPickerView, numberOfRowsInCompoment component: Int) ->Int{
return 5
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
return fruits[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
var fruitSelected = fruits[row]
switch (fruitSelected){
case "Apples":
imageView.image = UIImage(named: "apple.jpg")
infoLabel.text = "These apples are red"
case "Oranges":
imageView.image = UIImage(named: "orange.jpg")
infoLabel.text = "These oranges are yellow"
case "Lemons":
imageView.image = UIImage(named: "Lemon.jpg")
infoLabel.text = "These lemons are green"
case "Blueberries":
imageView.image = UIImage(named: "Blueberry.jpg")
infoLabel.text = "These blueberries are blue"
default:
imageView.image = UIImage(named: "fruit.jpg")
infoLabel.text = "Pick a fruit"
}
}
}
假设您已在情节提要中正确设置委托和数据源,您的问题是方法签名不正确。请注意您的方法签名中的拼写错误 (pickeView: UIPickerView, numberOfRowsInCompoment component: Int)
。 pickeView
中缺少 r
,numberOfRowsInCompoment
中使用 m
而不是 n
。
试试下面的函数,你应该已经准备好了...
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) ->Int{
return 5
}
我用的是最新版的Xcode,也是最新版的Swift。我按照此 link 的步骤进行操作,但 pickerview 没有出现在模拟器中。代码如下。有人可以帮我检查一下吗?非常感谢!
import UIKitclass ViewController: UIViewController, UIPickerViewDelegate {
@IBOutlet var imageView: UIImageView! @IBOutlet var infoLabel: UILabel! @IBOutlet var picker: UIPickerView! //var fruits: [String] = [String]() var fruits = ["Pick a fruit" , "Apples" , "Oranges" , "Lemons" , "Blueberries"] override func viewDidLoad() { super.viewDidLoad() imageView.image = UIImage(named: "fruit.jpg") // picker.delegate = self // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{ return 1 } func pickerView(pickeView: UIPickerView, numberOfRowsInCompoment component: Int) ->Int{ return 5 } func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{ return fruits[row] } func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){ var fruitSelected = fruits[row] switch (fruitSelected){ case "Apples": imageView.image = UIImage(named: "apple.jpg") infoLabel.text = "These apples are red" case "Oranges": imageView.image = UIImage(named: "orange.jpg") infoLabel.text = "These oranges are yellow" case "Lemons": imageView.image = UIImage(named: "Lemon.jpg") infoLabel.text = "These lemons are green" case "Blueberries": imageView.image = UIImage(named: "Blueberry.jpg") infoLabel.text = "These blueberries are blue" default: imageView.image = UIImage(named: "fruit.jpg") infoLabel.text = "Pick a fruit" } } }
假设您已在情节提要中正确设置委托和数据源,您的问题是方法签名不正确。请注意您的方法签名中的拼写错误 (pickeView: UIPickerView, numberOfRowsInCompoment component: Int)
。 pickeView
中缺少 r
,numberOfRowsInCompoment
中使用 m
而不是 n
。
试试下面的函数,你应该已经准备好了...
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) ->Int{
return 5
}