我使用了最新版本的Xcode以及最新版本的Swift。我按照link的步骤操作,但是选择器视图没有显示在模拟器中。代码如下。有人可以帮我查一下吗?非常感谢!
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" } } }
答案 0 :(得分:1)
假设您已在故事板中正确设置了委托和数据源,那么您的问题是方法签名不正确。请注意方法签名(pickeView: UIPickerView, numberOfRowsInCompoment component: Int)
中的拼写错误。 r
中pickeView
中缺少m
而n
中使用numberOfRowsInCompoment
而不是func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) ->Int{
return 5
}
。
尝试使用以下功能,你应该全部设置......
19 August 2015 - 10:50 am