在Segue之前添加完成处理程序

时间:2015-12-07 01:28:08

标签: ios swift

我怎样才能为这段代码添加一个完成处理程序,这样我才能确保视图在我得到" city"之前不会调用那个Segue。还是错误?

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let longitude = locManager.location?.coordinate.longitude
    let latitude = locManager.location?.coordinate.latitude
    print(latitude)
    let location = CLLocation(latitude: latitude!, longitude: longitude!)
    NSUserDefaults.standardUserDefaults().setDouble(longitude!, forKey: "longitude")
    NSUserDefaults.standardUserDefaults().setDouble(latitude!, forKey: "latitude")

    CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
        if error == nil {
            let pa = placemarks! as [CLPlacemark]

            var placeMark: CLPlacemark!
            placeMark = pa[0]

            if let city = placeMark.addressDictionary!["City"] as? String {
                print(city)
                NSUserDefaults.standardUserDefaults().setObject(city, forKey: "city")
            }
        } 
        else {
            print("Error: " + error!.localizedDescription)
        }
    })

    self.performSegueWithIdentifier("toMain", sender: self)
}

1 个答案:

答案 0 :(得分:1)

当您没有收到任何错误时,请在第一个if语句中拨打电话。

if( error == nil)
{
    // make sure value you're getting is correct and save to NSUserDefaults
    self.performSegueWithIdentifier("toMain", sender: self)
}
else
{
    // handle the error
}