2017-01-26 14 views
0

私は多くの質問と回答を見てきました。私のアプリが失敗し続ける理由を見つけることができません。不便をおかけして申し訳ありませんが、誰かが私を助けることができますか?私はあなたがbelow.theの問題のようにデリゲートを実装する必要があり、迅速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

コードの整合性をチェックする// stuffs.itが中かっこの外側にあり、viewdidloadに配置してプロジェクトをビルドします – karthikeyan

答えて

0

を使用していますと、あなたはUIPickerViewDataSource含むが、Xcodeのではないので、そのデータソース

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

これだけです。

関連する問題