[英]Swift does not conform to protocol
將使用Swift 6構建的應用程序拉到現在使用6 beta的系統后,我得到了“ EventFormViewController不符合協議UIPickerViewDataSource”的信息。 我已經為此苦苦掙扎了幾天,有什么建議嗎?
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
函數頭被注釋。
// returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return eventChoices.count }
應該是這樣的:
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
{
return eventChoices.count
}
2
您viewDidLoad
在viewDidLoad
實現之后關閉類定義。
應該是這樣的:
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.
}
*/
}
您遇到一些輸入錯誤:
在didReceiveMemoryWarning
之前,您需要使用一個方括號將類關閉。 因此,您的其他方法不在UIPickerViewDelegate的類中,因此委托認為,這些方法不存在。
因此,將方括號移到代碼末尾以關閉該類。
其次,在didReceiveMemoryWarning
方法之后,您會犯一個錯誤。 您已經注釋掉了方法標頭:
// returns the number of 'columns' to display. func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return eventChoices.count }
因此,將其更改為如下所示:
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return eventChoices.count
}
最后但並非最不重要的一點是,您需要重寫didReceiveMemoryWarning
方法。 因此更改:
func didReceiveMemoryWarning() {
didReceiveMemoryWarning() // Dispose of any resources that can be recreated.
}
對此:
override func didReceiveMemoryWarning() {
didReceiveMemoryWarning() // Dispose of any resources that can be recreated.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.