-2

I'm making a new application.

If I have a text field on one view controller and a label on another (both View Controllers in ViewController class), How can i type something in on the text field and then take me to the next ViewController and display the text?

Peter Theill
  • 3,117
  • 2
  • 27
  • 29
ReeRay
  • 1
  • 1
  • 4
  • [Suragach's answer](http://stackoverflow.com/a/31934786/4029561) on that question is specifically for swift – NSNoob Oct 28 '16 at 07:46

1 Answers1

2

In the source controller in prepare for segue you set a property on your destination controller:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let detailViewController = segue.destination as? DetailViewController {
            detailViewController.titleText = mylabel.text
        }
    }

In your destination controller's view did load you assign this property to your label field.

    var titleText = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        label.text = titleText
    }

Note you cannot assign directly to your label in prepareForSegue because the label is not guaranteed to have been initialized until ViewDidLoad has been called. Thats why the value goes into a property. Proper model view separation also dictates that one controller shouldn't be writing to another controller's view anyways.

Josh Homann
  • 15,933
  • 3
  • 30
  • 33