iOS Swift 在 NSS UrlSession Completion Handler 之后在 Segue 中传递数据
iOS Swift passing data in Seque following NSSUrlSession Completion Handler
我知道这个问题 ("passing data between views in swift?") 有很多变体,但我找不到适合我的情况的变体。这让我很担心,因为我担心我没有使用正确的设计模式。我喜欢有关设计模式的建议以及有关如何解决我的特定问题的提示。
问题背景:
我正在创建一个依赖于用户位置数据的应用程序。用户按下一个按钮,当他们按下按钮时,我获取他们的经纬度并将其传递给一个运行 NSURLSession 的单例实例到我的托管网站 API。 API returns JSON 响应,我的 NSURLSession 完成处理程序将其转换为 NSDictionary 对象,然后 returns 到我的按钮的完成处理程序以及状态代码。如果状态代码表示从我的网络 API 收到的数据是好的,那么我想通过 segue 将该数据传递给新的视图控制器,以便新的视图控制器可以格式化并将信息输出给用户。但是,如果状态显示有错误或 w/e 我想向用户输出适当的消息,并让他们保持在同一地图页面上,这样他们就可以 select 一个有效的位置(我没有没有实现这个,但假设它应该不会太难)。
我的问题:
我知道什么时候我想调用 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
}
}
在此先感谢您的帮助。如果您需要我提供任何其他信息,请告诉我。
这里不应该使用 sender
参数...但是,您所要做的就是调用 self.performSegueWithIdentifier("identifier", sender: appointments)
就在注释是您要继续的代码的地方。
然后在你的 prepareForSegue
中, sender 参数将有你的约会,你可以将它传递给下一个视图控制器。
我假设 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
}
}
我知道这个问题 ("passing data between views in swift?") 有很多变体,但我找不到适合我的情况的变体。这让我很担心,因为我担心我没有使用正确的设计模式。我喜欢有关设计模式的建议以及有关如何解决我的特定问题的提示。
问题背景:
我正在创建一个依赖于用户位置数据的应用程序。用户按下一个按钮,当他们按下按钮时,我获取他们的经纬度并将其传递给一个运行 NSURLSession 的单例实例到我的托管网站 API。 API returns JSON 响应,我的 NSURLSession 完成处理程序将其转换为 NSDictionary 对象,然后 returns 到我的按钮的完成处理程序以及状态代码。如果状态代码表示从我的网络 API 收到的数据是好的,那么我想通过 segue 将该数据传递给新的视图控制器,以便新的视图控制器可以格式化并将信息输出给用户。但是,如果状态显示有错误或 w/e 我想向用户输出适当的消息,并让他们保持在同一地图页面上,这样他们就可以 select 一个有效的位置(我没有没有实现这个,但假设它应该不会太难)。
我的问题:
我知道什么时候我想调用 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
}
}
在此先感谢您的帮助。如果您需要我提供任何其他信息,请告诉我。
这里不应该使用 sender
参数...但是,您所要做的就是调用 self.performSegueWithIdentifier("identifier", sender: appointments)
就在注释是您要继续的代码的地方。
然后在你的 prepareForSegue
中, sender 参数将有你的约会,你可以将它传递给下一个视图控制器。
我假设 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
}
}