尝试在视图不在窗口层次结构中的<vc>上显示<vc>

时间:2016-09-05 07:13:59

标签: swift

我正在尝试使用google api:

从邮政编码/邮政编码中获取lat长坐标
func getLatLngForZip(zipCode: String, completion: CLLocationCoordinate2D -> ())  {

    let url: NSURL = NSURL(string: "\(baseUrl)address=\(zipCode)&key=\(apikey)")!
    let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: url)
    let session = NSURLSession.sharedSession()

    let task = session.dataTaskWithRequest(urlRequest) {
        data, response, error -> Void in

        var locationCoordinates = CLLocationCoordinate2D()

        let httpResponse = response as! NSHTTPURLResponse
        let statusCode = httpResponse.statusCode
        if (statusCode == 200) {
            print("Everything is fine, GoogleAPI downloaded successfully.")

            do{
                let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
                if let result = json["results"] as? NSArray, let status = json["status"] as? String {

                    if status == "OK" {
                        if let geometry = result[0]["geometry"] as? NSDictionary {
                            if let location = geometry["location"] as? NSDictionary {
                                let latitude = location["lat"] as! Double
                                let longitude = location["lng"] as! Double
                                print("\n\(latitude), \(longitude)")
                                locationCoordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
                            }
                        }
                        dispatch_async(dispatch_get_main_queue()) {
                            completion(locationCoordinates)
                        }
                    } else {
                        let vc = PostCodeController()
                        let alert = UIAlertController(title: "Alert", message: "", preferredStyle: .Alert)
                        let action = UIAlertAction(title: "OK", style: .Default, handler: nil)
                        alert.addAction(action)
                        vc.presentViewController(alert, animated: true, completion: nil)

                    }
                }
            } catch let error {
                print("Error with GoogleAPI: \(error)")
            }
        }
    }
    task.resume()
}

简单地说,如果json的状态为“OK”,则输入的邮政编码/邮政编码是正确的,并且可以获取lat long。否则,邮政编码因某种原因无效,我想显示一条警告,告诉用户输入的邮政编码无效。

我理解else子句中的代码不应该存在。我觉得我应该从PostCodeController()内部发出警报,而不是现在的情况。

这就是它在PostCodeController()

中的调用方式
func tappedFindSalonButton(sender: UIButton!) {
    self.postCode = self.finderView.textField.text!
    fetchGoogleAPI()
}

func fetchGoogleAPI() {
    GoogleAPI().getLatLngForZip(finderView.textField.text!) {
        location in
        self.presentListControllerWithLocation(location)
    }
}
func presentListControllerWithLocation(locationCoordinate: CLLocationCoordinate2D) {

    let listController = ListController()
    listController.currentLocation = CLLocation(latitude: locationCoordinate.latitude, longitude: locationCoordinate.longitude)
    listController.postCode = postCode
    let vc = UINavigationController(rootViewController: listController)
    presentViewController(vc, animated: true, completion: nil)
}

为什么我遇到这种情况'尝试在“视图不在窗口层次结构中”“”“”我之前遇到过它,但还没有完全理解它的含义。

如果输入的邮政编码无效,我如何更改我的代码以显示提醒?

0 个答案:

没有答案