2016-04-24 9 views
0

この質問には多くのバリエーションがありました(「迅速なビュー間でデータを渡しますか?」)が尋ねましたが、私に固有のものを見つけることができませんでした状況。これは、私が適切なデザインパターンを使用していないことを恐れているため、私を心配しています。私はデザインパターンの提案だけでなく、私の特定の問題を解決するためのヒントを愛するだろう。iOS NSSUrlSession Completion Handlerの後にSequeでスウィフトデータを渡す

問題の背景:

私は、ユーザーの位置データに依存しているアプリを作成しています。ユーザーがボタンを押すと、その緯度と経度をとり、WebホストAPIにNSURLSessionを実行するシングルトンインスタンスに渡します。 APIは、NSURLSession補完ハンドラがNSDictionaryオブジェクトに変換するJSON応答を返します。このオブジェクトは、ステータスコードとともに、ボタンの補完ハンドラに戻されます。私のWeb APIから受け取ったデータが良好であることをステータスコードが示す場合、新しいView ControllerがSegueを介してそのデータを新しいView Controllerに渡して、新しいView Controllerがその情報をフォーマットしてユーザーに出力できるようにしたい。しかし、ステータスにエラーがあった場合やw/eが表示され、適切なメッセージをユーザーに出力して、同じマップページに保存して、有効な場所を選択できるようにしたい場合(これは実装されていませんが、それはあまりにも困難ではない)。

私の問題:

私はセグエを呼び出したいとき、私は知っていると私はそのセグエに渡したいというオブジェクトを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 
    } 
} 

ありがとう:

はここにボタンの私のコードとその完了ハンドラです。他の情報が必要な場合はお知らせください。

答えて

1

appointmentsはAPIが返す辞書です。完了ハンドラにappointmentsを格納するビューコントローラ用のプロパティを作成する必要があります。その後、performSegueWithIdentifier:senderに電話する必要があります。最後に、prepareForSegue:senderに、セグの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では、送信者引数にはあなたの予定があり、それを次のView Controllerに渡すことができます。

+0

なるほど...私はこの方法を示唆したチュートリアルを見ましたが、あなたはあなたが合格リスクコードの別の部分で同じセグエの識別子を呼び出すした場合ので、すべてのコメントは悪いデザインとしてそれを批判しました。悪いパラマター。すなわち、 2番目のビューコントローラでNSDictionaryの予定オブジェクトを期待していますが、nilを渡しています。あなたはもっと良い方法を知っていますか?そして、ハックのおかげで! –

+1

唯一の他の方法は、クラス変数を持って、完了ハンドラが呼び出されたときにそれを設定し、次にperformSegueを呼び出してから、prepareForSegueで次のVCに渡すものとしてグローバル変数を使うことができます – pbush25

関連する問題