2016-04-24 76 views
0

我知道這個問題存在很多變體(「在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 
    } 
} 

預先感謝任何幫助。如果您需要我的其他信息,請告訴我。

回答

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

這不完全是應該在這裏使用sender參數的方式......但是,所有您需要做的就是撥打self.performSegueWithIdentifier("identifier", sender: appointments)正確的地方,您的代碼需要註釋的地方是。 然後在你的prepareForSegue發件人參數將有你的約會,你可以傳遞給下一個視圖控制器。

+0

嗯......我看到一個教程,是在暗示這個方法,但所有的評論都指責其糟糕的設計,因爲如果你要調用相同SEGUE標識符的代碼另一部分,你的風險傳遞一個糟糕的參議員。即。你期待在第二個視圖控制器中的NSDictionary約會對象,但你傳遞一個零。你知道更好的方法嗎?並感謝黑客! –

+1

那麼唯一的辦法是有一個類變量,並在完成處理程序被調用時設置它,然後調用performSegue,然後在您的prepareForSegue中,您可以使用全局變量作爲傳遞給下一個vc的全局變量 – pbush25

相關問題