iOS Swift在Seque之后在NSSUrlSession完成处理程序中传递数据

问题描述:

我知道这个问题存在很多变体(“在swift之间传递视图之间的数据?”),但我一直无法找到一个与我的情况。这让我很担心,因为我担心我没有使用适当的设计模式。我会喜欢设计模式的建议以及如何解决我的具体问题的提示。iOS Swift在Seque之后在NSSUrlSession完成处理程序中传递数据

问题背景:

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

我的问题:

我知道当我想叫赛格瑞,我知道的NSDictionary对象,我想传递给SEGUE。我只是不知道如何发送它。

这里是我的按钮的代码和它的完成处理:

// 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 
    } 
} 

预先感谢任何帮助。如果您需要我的其他信息,请告诉我。

我假设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 
    } 
} 

这不完全是应该在这里使用sender参数的方式......但是,所有您需要做的就是拨打self.performSegueWithIdentifier("identifier", sender: appointments)正确的地方,您的代码需要注释的地方是。 然后在你的prepareForSegue发件人参数将有你的约会,你可以传递给下一个视图控制器。

+0

嗯......我看到一个教程,是在暗示这个方法,但所有的评论都指责其糟糕的设计,因为如果你要调用相同SEGUE标识符的代码另一部分,你的风险传递一个糟糕的参*。即。你期待在第二个视图控制器中的NSDictionary约会对象,但你传递一个零。你知道更好的方法吗?并感谢黑客! –

+1

那么唯一的办法是有一个类变量,并在完成处理程序被调用时设置它,然后调用performSegue,然后在您的prepareForSegue中,您可以使用全局变量作为传递给下一个vc的全局变量 – pbush25