Icculus
Icculus

Reputation: 35

Passing multiple data between view controllers

EDITED

I have been having some issue with passing multiple label.text to another viewController in swift. I can pass one label between the two controllers fine, but I can not make it work to pass multiple. Usually when I try and pass multiple labels when the segue finishes the label on the other side just says label (the name of the the label passing) but not the answer that is supposed to be in the label. If I run it without the segue all the labels post exactly what they should. for instance:

      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

I have also tried this as making a new let for each one (let breakviewcontroller, breakviewcontroller1... ect) I am sure I have the other view controller set up properly becuase anytime I am sending one I can get it to work. I just started writing code and all of my knowledge has coming from reading things on the internet. Any help would be greatly appreciated. Thanks

Upvotes: 1

Views: 1243

Answers (1)

Hasya
Hasya

Reputation: 9898

With 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>()

// Now use this dictWithDetails in viewWillAppear or viewDidLoad, your all objects from sender class ( CurrentViewController ) will be available here

Without 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)

}

Upvotes: 2

Related Questions