在视图控制器之间传递多个数据

时间:2016-04-28 05:40:15

标签: ios swift uiviewcontroller segue

EDITED

我在将多个label.text传递给swift中的另一个viewController时遇到了一些问题。我可以很好地在两个控制器之间传递一个标签,但我不能让它传递多个。通常当我尝试传递多个标签时,segue完成另一侧的标签只是说标签(标签传递的名称),而不是应该在标签中的答案。如果我在没有segue的情况下运行它,所有标签都会发布它们应该的内容。例如:

      override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let breakviewcontroller: BreakAnswer = segue.destinationViewController as! BreakAnswer

    breakviewcontroller.ubrlab1 = ubrLabel1.text!
    breakviewcontroller.ubrlab2 = ubrLabel2.text!
    breakviewcontroller.ubrlab3 = ubrLabel3.text!



}

//this is the other view controller
@IBOutlet weak var ubrAnswer1: UILabel!

@IBOutlet weak var ubranswer2: UILabel!

@IBOutlet weak var ubranswer3: UILabel!



var ubrlab1: String = ""
var ubrlab2: String = ""
var ubrlab3: String = ""








override func viewDidLoad() {
super.viewDidLoad()


ubrAnswer1.text = ubrlab1
ubranswer2.text = ubrlab2
ubranswer3.text = ubrlab3

我也试过这个为每个人做一个新的让(让breakviewcontroller,breakviewcontroller1 ...等) 我确信我有其他视图控制器设置正确,因为我发送一个我可以让它工作。 我刚刚开始编写代码,我的所有知识都来自于在互联网上阅读的东西。任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:2)

使用segue

// CurrentViewController

var dictWithDetails = Dictionary<String, AnyObject>()

self.performSegueWithIdentifier("SegueFromCurrentViewControllerToNewViewController", sender: dictWithDetails)


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

    let objNextViewController : NextViewController =  segue.destinationViewController as! NextViewController

    var dict :Dictionary <String,AnyObject> = (sender as? Dictionary)!

    objNextViewController.dictWithDetails=dict

}

// NewViewController

var dictWithDetails = Dictionary<String, AnyObject>()

//现在在viewWillAppear或viewDidLoad中使用这个dictWithDetails,你的发送者类(CurrentViewController)中的所有对象都可以在这里使用

没有seque

@IBAction func clickToNextViewController(sender: UIButton) {
{

    let objNextViewController = self.storyboard?.instantiateViewControllerWithIdentifier(“ID_NextViewController”) as? NextViewController

    // objNextViewController.properties1 = currentClassproperties1
    // objNextViewController.properties2 = currentClassproperties2

    self.navigationController?.pushViewController(objNextViewController!, animated: true)

}