iOS Swift按照NSSUrlSession完成处理程序

时间:2016-04-24 21:44:36

标签: ios swift segue completionhandler

我知道这个问题有很多变化(“在swift中的视图之间传递数据?”)但是我找不到一个特定于我的情况的问题。这让我担心,因为我担心我没有使用正确的设计模式。我会喜欢有关设计模式的建议以及如何解决我的具体问题的提示。

问题背景:

我正在创建一个依赖于用户位置数据的应用。用户按下按钮,当他们这样做时,我将他们的纬度和经度传递给运行NSURLSession到我的Web托管API的单例实例。 API返回一个JSON响应,我的NSURLSession完成处理程序转换为NSDictionary对象,然后它返回到我的按钮的完成处理程序以及状态代码。如果状态代码表示从我的Web API收到的数据是好的,那么我想通过segue将该数据传递给新的视图控制器,以便新的视图控制器可以格式化并将信息输出给用户。但是,如果状态显示有错误或w / e我想向用户输出适当的消息,并将它们保存在同一个地图页面上,以便他们可以选择一个有效的位置(我没有实现这个但是假设这不应该太难。)

我的问题:

我知道当我想调用一个segue时,我知道我要传递给那个segue的NSDictionary对象。我只是不知道如何发送它。

这是我的按钮及其完成处理程序的代码:

// MARK: ACTIONS
@IBAction func scheduleAppointmentButton(sender: UIButton) {
   //grab async manager singleton
    let theAsyncManager = AsyncManager.sharedAsyncManager
    //Send HTTP Request to API to Get all available Appointments
    if let lon = selectedPin?.coordinate.longitude, lat = selectedPin?.coordinate.latitude{
        theAsyncManager.httpGetAppointments(lat, lon: lon)
            {
                (appointments, status) -> () in
                if(status == "All Good"){
                    //Want to trigger segue here and send it appointments object
                }
            }
    } else{
        //there was an error here getting the lat and lon
    }
}

提前感谢您的帮助。如果您需要我提供任何其他信息,请与我们联系。

2 个答案:

答案 0 :(得分:1)

这不完全是sender参数应该在这里使用的方式......但是,你所要做的只是在你的代码为segue的地方调用self.performSegueWithIdentifier("identifier", sender: appointments)是。 然后在你的prepareForSegue中,sender参数将有你的约会,你可以将它传递给下一个视图控制器。

答案 1 :(得分:1)

我假设appointments是您的API返回的字典。您应该为视图控制器创建一个属性,将appointments存储在完成处理程序中。然后你应该致电performSegueWithIdentifier:sender。最后,在prepareForSegue:sender中,将字典分配给segue destinationViewController的属性。类似的东西:

class YourViewController: UIViewController {

    var appointments: NSDictionary?

    @IBAction func scheduleAppointmentButton(sender: UIButton) {
        //grab async manager singleton
        let theAsyncManager = AsyncManager.sharedAsyncManager
        //Send HTTP Request to API to Get all available Appointments
        if let lon = selectedPin?.coordinate.longitude, lat = selectedPin?.coordinate.latitude{
            theAsyncManager.httpGetAppointments(lat, lon: lon) {
                (appointments, status) -> () in
                if(status == "All Good"){
                    //Want to trigger segue here and send it appointments object
                    self.appointments = appointments

                    // Remember to perform the segue on the main thread
                    dispatch_async(dispatch_get_main_queue(), {
                        self.performSegueWithIdentifier("NameOfYourSegue", sender: nil)
                    })
                }
            }
        } else{
            //there was an error here getting the lat and lon
        }
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let destination = segue.destinationViewController as! YourDestinationClassName
        destination.appointments = appointments
    }
}