繁体   English   中英

viewController不符合协议UIPickerViewDataSource我的代码有什么问题?

[英]viewController does not conform to protocol UIPickerViewDataSource what is wrong with my code?

我已经浏览了许多问题和答案,但找不到我的应用程序不断失败的原因。 很抱歉给您带来不便,但是有人可以帮我吗? 我正在使用Swift 3。

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate, UIPickerViewDelegate, UIPickerViewDataSource {

    //Distance
    @IBOutlet weak var setDistance: UIPickerView!

    var distance = ["1/2 Mile", "1 Mile", "2 Miles", "5 Miles"]

    //Map
    @IBOutlet weak var map: MKMapView!


    let manager = CLLocationManager()

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        let location = locations[0]

        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
        map.setRegion(region, animated: true)

        print(location.altitude)
        print(location.speed)

        self.map.showsUserLocation = true
    }



    override func viewDidLoad()
    {
        super.viewDidLoad()

        //Distance Stuff
        setDistance.delegate = self
        setDistance.dataSource = self

    }

        func numberOfComponents(in: UIPickerView) -> Int {
            return 1
        }
        func setDistance(_ setDistance: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            return distance.count
        }
        func setDistance(_ setDistance: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String {
            return distance[row]
        }


        //Map Stuff
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()








    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

您应该像下面那样实现委托。问题说,您正在包含UIPickerViewDataSource但未实现其数据源

import UIKit
import MapKit
import CoreLocation

class TestViewController: UIViewController, CLLocationManagerDelegate, UIPickerViewDelegate, UIPickerViewDataSource {

    //Distance
    @IBOutlet weak var setDistance: UIPickerView!

    var distance = ["1/2 Mile", "1 Mile", "2 Miles", "5 Miles"]

    //Map
    @IBOutlet weak var map: MKMapView!


    let manager = CLLocationManager()

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        let location = locations[0]

        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
        map.setRegion(region, animated: true)

        print(location.altitude)
        print(location.speed)

        self.map.showsUserLocation = true
    }



    override func viewDidLoad()
    {
        super.viewDidLoad()

        //Distance Stuff
        setDistance.delegate = self
        setDistance.dataSource = self
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()


    }

   /* func numberOfComponents(in: UIPickerView) -> Int {
        return 1
    }
    func setDistance(_ setDistance: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return distance.count
    }
    func setDistance(_ setDistance: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String {
        return distance[row]
    }
 */
    func numberOfComponents(in: UIPickerView) -> Int {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
       return distance.count
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
         return distance[row]
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

由于Xcode无法实现或建议所需的功能,因此我发现最好的识别方法是使用文档和API参考。

您可以在协议上显示快速帮助(Option +单击),然后单击“协议参考”:

在此处输入图片说明

这将打开文档和API参考,其中向您详细介绍了必需的功能:

在此处输入图片说明

您还可以搜索所需的关键作品(CMD + F)并在搜索字段中键入所需的内容。

然后,您可以在您的课程中实现它们:

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return dataset.count
}

Xcode帮助您找到相关功能,例如,开始键入pickerview ,它将为您提供协议功能列表:

在此处输入图片说明

而已。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM