可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
After pulling an app that was built using Swift 6 to now a system that is using 6 beta, I'm getting an "EventFormViewController does not conform to protocol UIPickerViewDataSource". I have struggled with this for several days now, any suggestions?
import UIKit var eventChoices = [ ["5","10","15","30","45","60","90","120","150","180"], ["Hospital Committee","Peer Review","EHR Improvement","Quality Improvement","Business Development"], ] class EventFormViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate{ @IBOutlet weak var eventPicker: UIPickerView! @IBOutlet weak var eventLabel: UILabel! @IBOutlet weak var commentField: UITextField! func updateLabel(){ let selectedTime = eventChoices[0][eventPicker.selectedRowInComponent(0)] let event = eventChoices[1][eventPicker.selectedRowInComponent(1)] eventLabel.text = "Chose \(event) for \(selectedTime) mins" } func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { updateLabel() } override func viewDidLoad() { super.viewDidLoad() } // Do any additional setup after loading the view. } func didReceiveMemoryWarning() { didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return eventChoices.count } // returns the # of rows in each component.. func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{ return eventChoices[component].count } func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { return eventChoices[component][row] } func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { if (component == 0) { return 50.0; } return 300.0; } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */
回答1:
There are alot of issues in your code.
1
The function header is commented.
// returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return eventChoices.count }
It should be like:
// returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return eventChoices.count }
2
You are closing your class definition right after the viewDidLoad
implementation.
It should be like:
import UIKit var eventChoices = [ ["5","10","15","30","45","60","90","120","150","180"], ["Hospital Committee","Peer Review","EHR Improvement","Quality Improvement","Business Development"], ] class EventFormViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate { @IBOutlet weak var eventPicker: UIPickerView! @IBOutlet weak var eventLabel: UILabel! @IBOutlet weak var commentField: UITextField! func updateLabel() { let selectedTime = eventChoices[0][eventPicker.selectedRowInComponent(0)] let event = eventChoices[1][eventPicker.selectedRowInComponent(1)] eventLabel.text = "Chose \(event) for \(selectedTime) mins" } func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { updateLabel() } override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return eventChoices.count } // returns the # of rows in each component.. func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return eventChoices[component].count } func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! { return eventChoices[component][row] } func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { var width = 300.0 if (component == 0) { width = 50.0; } return width; } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ }
回答2:
You've got some typing-errors:
Before your didReceiveMemoryWarning
you've got a close bracket which closes the class. So your other methods aren't in the class with the UIPickerViewDelegate and so the delegate thinks, that the methods doesn't exist.
So move the bracket to the end of your code to close the class.
Secondly, you've got a mistake after the didReceiveMemoryWarning
method. You've got outcommented a method header:
// returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return eventChoices.count }
So change that to look like that:
// returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int { return eventChoices.count }
And last but not least you need to override the didReceiveMemoryWarning
method. So change that:
func didReceiveMemoryWarning() { didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }
to that:
override func didReceiveMemoryWarning() { didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }