2017-01-26 2 views
0

많은 질문과 답변을 살펴본 결과 내 앱이 계속 실패하는 이유를 찾을 수 없습니다. 불편을 끼쳐 드려 죄송합니다. 누군가 나를 도와 줄 수 있습니까? 나는 빠른 3.viewController가 프로토콜 UIPickerViewDataSource를 준수하지 않습니다. 코드에 어떤 문제가 있습니까?

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() 
    } 

} 
+0

코드 정렬 확인 // stuff.it가 중괄호 밖에 있지만 viewdidload에 넣고 프로젝트를 빌드하십시오. – karthikeyan

답변

0

당신은 below.the 문제와 같은 대리인이, 당신이 자신의 데이터 소스 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() 
    } 

} 
0

가 아닌 사용하고 필요한 기능을 구현하거나 제안 할 수있는 가장 좋은 방법은 문서 및 API 참조를 사용하는 것입니다. 당신은 프로토콜에 빠른 도움을 표시 할 수 있습니다

(옵션 + 클릭) 한 다음 프로토콜 참조를 클릭 :

enter image description here

을 그리고 이것은 문서 및 당신에게 필요한 기능에 대한 자세한 내용을 알려줍니다 API 참조를 열 것입니다 :

enter image description here

또한 검색 필드에 필수 필수 핵심 작업 (CMD + F)와 유형을 검색 할 수 있습니다.

그런 다음 당신은 당신의 클래스를 구현할 수 있습니다

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

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

Xcode를 사용하면, 관련 기능을 찾을 예를 들어, pickerview 입력을 시작하는 데 도움이 당신에게 프로토콜 기능의 목록을 제시 할 것이다 :

enter image description here

그게 전부입니다.