Ios – Passing Data in Swift Between View Controllers in Same File

iossegueswiftuiviewcontroller

Why can't I pass data from one view controller class to another view controller class when they are in the same .swift file?

I've done both of the tutorials linked bellow in an effort to understand passing data, and both have you make another .swift file for you second view controller. When I followed them exactly it works. When I do everything the same but just put the class SecondViewController in the same .swift file as the class FirstViewController it doesn't work. Why?

I'm building an application with three view controllers and I'd rather just keep all the code in one file if I can. Is this why it's not working or is it just bad form?

Tutorials:

How to Pass Data from View Controller (Swift : Xcode) on YouTube

Segue Between Swift View Controllers on CodingExplorer

This works:

//  ViewController.swift
import UIKit
class ViewController: UIViewController {
    @IBOutlet var TextField: UITextField!    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var DestViewController: ViewTwo = segue.destinationViewController as ViewTwo        
        DestViewController.LabelText = TextField.text        
    }
}

//  ViewTwo.swift
import UIKit
    class ViewTwo: UIViewController {
    @IBOutlet var Label: UILabel!    
    var LabelText = String()    
    override func viewDidLoad() {        
        Label.text = LabelText       
    }
}

This does not:

//  ViewController.swift
import UIKit

class ViewController: UIViewController {
    @IBOutlet var TextField: UITextField!    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var DestViewController: ViewTwo = segue.destinationViewController as ViewTwo        
        DestViewController.LabelText = TextField.text        
    }
}

class ViewTwo: UIViewController {
    @IBOutlet var Label: UILabel!    
    var LabelText = String()    
    override func viewDidLoad() {        
        Label.text = LabelText       
    }
}

Best Answer

Working fine for me.

  1. Have you set the classes for your views properly? The view with your textField (and probably a button to ViewTwo) should be set to ViewController and your second view with the label should be set to ViewTwo.

  2. Check the connections between your label/textField. Are they properly connected to your class?

  3. This is the code I used, which should be exactly the same:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var TextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var DestViewController = segue.destinationViewController as! ViewTwo
        DestViewController.LabelText = TextField.text
    }
}

class ViewTwo: UIViewController {

    @IBOutlet var Label: UILabel!

    var LabelText = String()

    override func viewDidLoad() {
        Label.text = LabelText
    }
}
Related Topic