Swift不符合协议

时间:2015-02-08 22:44:28

标签: ios swift

将使用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.
    }
    */

2 个答案:

答案 0 :(得分:3)

您的代码中存在很多问题。

<强> 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实施后立即关闭班级定义。

应该是这样的:

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.
    }
    */
}

答案 1 :(得分:1)

你有一些输入错误:

在你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.
}