2016-04-19 2 views
-6

セグを作成して別のビューコントローラにデータを渡したいが、セグが発生する特定の条件がある。可能であれば、私はドラッグ方法の代わりにセグーIDを使用することを好むでしょう。特定の基準が満たされている場合にのみ、データを別のView Controllerに渡すセグを作成するには

これは

@IBAction func SubmitButtonPressed(sender: AnyObject) { 

    if 1<0 { 
     // dont perform segue 
    }else{ 
     //Perform segue 

     // i want to pass this data in the next VC 
     var data = "foo" 

     //this is my segue id i want o use to go to the Second VC 
     var segueId = "segueForgotPasswordTwo" 

     // second VC 
     var secondVc = "viewController2" 


     // Iwant to to use prepare for segue but im getting errors in the parameters 
     prepareForSegue(UIStoryboardSegue, sender: AnyObject?){ 


     } 

    } 
} 
+0

これはあまりにも広すぎます - あなたのコードサンプルは私をさらに混乱させるだけです。どのようなsegue ID、どのような "ドラッグ方法"?一般的に、 'prepareForSegue'メソッドの中にいくつかのデータを渡すだけです。そこに条件付きロジックを置くことができます。 – luk2302

+0

フォームを作成する前に、View Controller1が次のView Controller2 iにセグメンテーションする前に、フォームを作成します。フォームが空の場合、View Controller 1に空きが残っていない場合、次のView Controllerに移動したいと思っています。私は助けることができたことを願っています –

+4

あなたはそれを正確にやることを止めていますか?いくつかのIBActionコードにフォーム送信ボタンアクションをフックし、何らかの確認を行い、入力が有効であれば、セグをトリガーします。 – luk2302

答えて

2

あなたの質問は少し不明であるが、私は、これはあなたが探しているものであると信じを達成しようとしてイム...

func someFunction(){ 

     if //some condition { 
     //code 

     }else if //some condition { 
     //code 

     } else { 
     //perform segue by using the folowing line. Assign the identifier to the segue in the storyboard. 
      //Do this by first creating a segue by dragging from a view controller to the destination view controller. Be sure to drag from the VIEWCONTROLLER, to the destination VIEWCONTROLLER. DO NOT just drag from the button. Next, choose the type of segue (eg. show or present modally), and then type in an indentifier for this segue. 
     performSegueWithIdentifier("SegueIdentifier", sender: nil) 
    } 



    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if segue.identifier == "SegueIdentifier" { 

      //now find your view controller that you are seguing to. 
      let controller = segue.destinationViewController as! SomeViewController 

      //access the properties of your viewController and set them as desired. this is how you will pass data along 
      controller.property = someValue 

     } 
    } 
1

概要の例です:

  1. ソースビューコントローラからデスティネーションビューコントローラにセグをフックします(左側の赤い矢印を参照)
  2. はセグエその後、実行カスタム条件チェックを行うためのボタンのアクションを作成し、宛先ビューコントローラ
  3. にあるボタンから、それをフックしないでください

スクリーンショット:

enter image description here

コード:

var data = "foo" 

    @IBAction func buttonPressed(sender: UIButton) { 

     let someCondition = true 

     if someCondition { 
      performSegueWithIdentifier("showGreen", sender: self) 
     } 
     else { 
      performSegueWithIdentifier("showPink", sender: self) 
     } 
    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if segue.identifier == "showGreen" { 

      let greenVC = segue.destinationViewController as! GreenViewController 
      // Make sure the data variable exists in GreenViewController 
      greenVC.data = data 

     } 
    } 
1

shouldPerformSegueWithIdentifierあなたのViewControllerの関数です。セグがトリガーされたとき、この関数はfalseを返すとセグを取り消すことができるので、この関数に必要なロジックをインクルードして、必要に応じてtrue/falseを返すことができます。

関連する問題