如何正确地从pickerView转到MapKitView

时间:2017-02-05 22:01:13

标签: ios swift segue

致敬,

所以我在我的主ViewController中有一个pickerView,在我的ViewControllerTwo中我有一个单独的mapKitView。我的问题是,当我尝试通过单击我的按钮进行segue时,我的应用程序崩溃并抛出异常。只是一个NSException,这当然不会给我太多。

我处理我的segue的方法是我通过手动将一个从VC1拖到VC2创建一个并给它标识符" viewMapSegue"然后我也给了我的按钮标签为1,因为它是我是如何进行上一次练习的,并认为通过这样做,我可以确保将值设置为我在segue时而不是之后需要的值。

所有这一切,我不确定我在哪里犯了错误。我知道如果我自己使用我的VC2代码和一些硬编码的lat / long值,它运行正常并显示我输入的位置。代码如下:

CityData:

select

VC1:

import Foundation

class CityData {
    var cityName:String;
    var cityHighLight:String;
    var cityLat:Double;
    var cityLong:Double;

    init(cityName: String, cityHighLight:String, cityLat:Double, cityLong:Double) {
        self.cityName = cityName;
        self.cityHighLight = cityHighLight;
        self.cityLat = cityLat;
        self.cityLong = cityLong;
    }
}

最后是mapview所在的VC2:

import UIKit


class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{

    var globalRow : Int = 0;


    @IBOutlet weak var cityPicker: UIPickerView!;

    @IBAction func viewMap(button: UIButton) {
        performSegue(withIdentifier: "viewMapSegue", sender: button);
    }


    var data:[CityData] =
    [
        CityData(cityName:"Waycross", cityHighLight:"Alligators", cityLat: 31.214893, cityLong:-82.357244),
        CityData(cityName:"Athens", cityHighLight:"Heaven on Earth", cityLat: 33.946955, cityLong:-83.377878),
        CityData(cityName:"Plains", cityHighLight:"Peanuts", cityLat: 32.036008, cityLong:-84.393335),
        CityData(cityName:"Milledgeville", cityHighLight:"Mental Hospital", cityLat: 33.081313, cityLong:-82.233013),
        CityData(cityName:"Wrightsville", cityHighLight:"Home of Herschel", cityLat: 32.728958, cityLong:-82.719602)
    ];

//    var cityData:[String] = ["Waycross", "Plains", "Athens", "Milledgeville", "Wrightsville"];
//    var cityHighlights:[String] = ["Alligators", "Peanuts", "Heaven on Earth", "Mental Hospital", "Home of Herschel"];



    //number of columns
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1;
    }

    //number of rows
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return data.count;
    }

    //title for row
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        //dataFromPicker.text = data[row].cityHighLight;
       globalRow = row;

        return data[row].cityName;

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.cityPicker.dataSource = self;
        self.cityPicker.delegate = self;
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let lat = segue.destination as! ViewControllerTwo;
        let long = segue.destination as! ViewControllerTwo;

        if let button = sender as? UIButton {
            if button.tag == 1 {
                lat.latChosen = data[globalRow].cityLat;
                long.longChosen = data[globalRow].cityLong;

            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

This is how the storyboard looks.

1 个答案:

答案 0 :(得分:1)

哇....我不敢相信我没想过早点检查一下。所以。显然我的按钮有两个链接,一个是连接的应该和另一个来自上帝知道在哪里。哇....

你知道,苹果真的应该发出警告,例如"嘿,查看你的链接,兄弟"。无声的错误可能会让人疯狂。