如何在swift中将数据传递给多个ViewController

时间:2016-01-26 08:06:13

标签: swift

我想将例如ViewController“A”的数据传递给ViewController“B”,并在点击“OK”按钮时将另一个数据从“A”传递给“C”。我怎么能这样做?

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    println("YES")

    var mojodiTemp : B = segue.destinationViewController as B        println("NO")
    mojodiTemp.tempMojoodi = mablagh.text!

    var HesabeHarFard : C = segue.destinationViewController as C        
    HesabeHarFard.person = person
    HesabeHarFard.year = year
    HesabeHarFard.month = month
    HesabeHarFard.day = day

}

2 个答案:

答案 0 :(得分:0)

这篇文章中有一个详细的例子:

Best temporary storage measure

总结:

使用performSegueWithIdentifier并使用prepareForSegue来支持它,如下所示:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
            if (segue.identifier == "segueTitle") {
                // pass data to next view
                let destinationVC = segue.destinationViewController as! YourNextViewController
                destinationVC.transferObject = self.transferObject;

        }
    }

答案 1 :(得分:0)

首先使用适当的dataType在viewControllerB和C中创建dataToPass类变量/属性,然后按照以下步骤操作:

解决方案1:

//在ViewControllerA中,在创建之前编写以下函数,需要从A到B和A到C的段,每个段都有标识符。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "identifierForB" {
        let instanceOfViewControllerB = segue.destinationViewController as! ViewControllerB
        instanceOfViewControllerB.dataToPass = dataOne
    } else if segue.identifier == "identifierForC" {
        let instanceOfViewControllerC = segue.destinationViewController as! ViewControllerC
        instanceOfViewControllerB.dataToPass = nextData
    }
}

解决方案2:

如果您通过代码转到ViewControllerB或C:请执行以下操作:(在按下OK按钮的操作中保持此代码)

//First you need to give storyboard id to view controller associated with ViewControllerB and C then in action function of OK button
  let storyBoard = UIStoryboard(name: "Main", bundle: nil)
  if (showViewControllerB == true) {
     let instanceOfViewControllerB = storyBoard.instantiateViewControllerWithIdentifier("ViewControllerBIdentifier") as! ViewControllerB
     instanceOfViewControllerB.dataToPass = dataOne
     self.presentViewController(instanceOfViewControllerB, animated: true, completion: nil)
  } else if showViewControllerC == true {
     let instanceOfViewControllerC = storyBoard.instantiateViewControllerWithIdentifier("ViewControllerCIdentifier") as! ViewControllerC
     instanceOfViewControllerC.dataToPass = dataOne
     self.presentViewController(instanceOfViewControllerC, animated: true, completion: nil)
  }

解决方案3

您在A类中创建了两个属性:

var vcB: B!
var vcC: C!

如果恰好低于A类的定义 现在单击确定按钮:

 let storyBoard = UIStoryboard(name: "Main", bundle: nil)
 vcB = storyBoard.instantiateViewControllerWithIdentifier("ViewControllerBIdentifier") as! ViewControllerB
 vcB.dataToPass = dataOne

 vcC = storyBoard.instantiateViewControllerWithIdentifier("ViewControllerCIdentifier") as! ViewControllerC
 vcC.dataToPass = dataOne

当您要在屏幕中打开C视图控制器时,请确保显示vcC,并确保在要打开B视图控制器时显示vcB。 当您单击按钮或从触发器显示C的视图时:只需将以下代码放入函数/操作:

self.presentViewController(vcC, animated: true, completion: nil)

当您单击按钮或从触发器显示B的视图时:只需将以下代码放入函数/操作:

        self.presentViewController(vcB, animated: true, completion: nil)

希望这就是你想要的,是吗?