0

I'm trying to change the title in a TableViewController from another ViewController. (see image)

ViewControllers

The second ViewController is the one with the 3 cells and the third one is the one with a textfield (inputText in code), a button (changeText) and a label (outputLabel). I would like this app to remember what I put in the text field when I go back to the table view and then back into the ViewController. What happens now is:
- I change the text, hit the button and the label changes.
- I go back to the TableViewController and then I go into the ViewController that I was just in with a changed label
- The label is what it was before...

How can I make the app 'remember' what I put in in the text field and what the label was like? My code (ViewController.swift, I linked the 3rd controller to this file, haven't linked the 2nd controller to anything (yet?)):

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var outputLabel: UILabel!
@IBOutlet weak var inputText: UITextField!
@IBAction func changeText(_ sender: UIButton) {
    outputLabel.text = inputText.text
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

Thanks in advance!

G Buis
  • 303
  • 4
  • 17

1 Answers1

0

You can reference your ViewController in first controller (TableViewController), make public inputText

@IBOutlet public  weak var inputText: UITextField!

and in viewDidAppear get your text

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let text = ViewControllerVar.inputText.text //your text
}
Scinfu
  • 1,081
  • 13
  • 18
  • Now, I don't really get where to place what text? I put `@IBOutlet public weak var inputText: UITextField!` in TableViewController.swift and I put `override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) let text = ViewControllerVar.inputText.text //your text }` in ViewController.swift but it doesn't work? – G Buis Apr 06 '17 at 15:23
  • 1
    You should only change your already defined inputText in ViewController to public and access from TableviewController to the reference – Scinfu Apr 07 '17 at 07:51