Displaying text entered into textfield (in second view controller) in a label on first view controller?

timman

I have been trying a challenge out alone in Swift to do programmatically - with no use of storyboards.

My first view controller has a label and a button to go to the second view controller.

I created a navigation controller within the app delegate file, and then push to the second view controller when the button is pressed.

The second view controller has a textfield, a button to save the text, and a button to go back to the first view controller.

The UI is fine, but I cannot figure out how to save the text that is entered into the textfield which is on the second view controller, and then display this text in the label in the first view controller.

Can someone give me a clue as to how to do this?

Currently in my button going back to the first view controller I have this:

func handleBackToVC1() {

        self.navigationController?.popViewController(animated: true)
    }

Is this where I pass the data that is in the textfield and display it in the label? Also, should I give the label some text to start off with? But which would then get changed to what was entered into the textfield?

Suhit Patil

use protocol delegate for passing data from SecondViewController to FirstViewController

protocol TextFieldDataDelegate: class {
    func saveText(_ text: String)
}

class SecondViewController: UIViewController {
   weak var delegate: TextFieldDataDelegate?

  //on save button tap call the delegate method
  delegate?.saveText(textField.text)
}

//set FirstViewController as delegate for TextFieldDataDelegate protocol

class FirstViewController: UIController, TextFieldDataDelegate {

 // when you create instance of SecondViewController assign the delegate as self
 let secondVC = SecondViewController()
 secondVC.delegate = self

  //in saveText set label text as passed from SecondVC
  func saveText(_ text: String) {
    self.textLabel.text = text
  }
}

Note: There are many similar questions like this, always search before asking the question. Thanks

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pass textfield to a label in a second view controller in swift

From Dev

Unable to embed second view controller on top of first view controller

From Dev

Dismissing a View Controller and displaying another View Controller

From Dev

How to pass label data to another label in the second view controller

From Dev

Passing data from the second view into the first view controller

From Dev

Passing data from the second view into the first view controller

From Dev

Modal View controller not displaying at Center

From Dev

Modal View controller not displaying at Center

From Dev

Passing Data from a View Controller to a First Detail Controller and then to a Second Detail Controller

From Dev

Updating Label of a View Controller by a NSDictionary

From Dev

Moving from first view controller to second after 5 seconds

From Dev

NavigationBar not appearing in second view controller

From Dev

Second view controller has no data

From Dev

iOS 7 : How to change the status bar text color as white in one view controller and black in second view controller?

From Dev

Save text of label when viewing another view controller

From Dev

Second view not appearing within view controller

From Dev

cast text from tableview with 2 sections to second view controller

From Dev

Moving to a second view controller from one view controller programatically

From Dev

Moving to a second view controller from one view controller programatically

From Dev

ng-view is not displaying the pages but controller is working

From Dev

Assistant Editor not displaying correct view controller?

From Dev

Displaying an URL from a different view controller in a UIWebView

From Dev

Displaying the Initial View Controller as a Form Sheet

From Dev

Displaying an URL from a different view controller in a UIWebView

From Dev

NameError in Controller and View is only displaying html

From Dev

Displaying a partial view from another controller

From Dev

How to pass a value from second view to first view through tab bar controller

From Dev

How to run second view controller only when data from server is loaded in coredata from first view controller in swift

From Dev

How can I use text from a table view cell and display it as a label in a parent view controller?

Related Related

  1. 1

    Pass textfield to a label in a second view controller in swift

  2. 2

    Unable to embed second view controller on top of first view controller

  3. 3

    Dismissing a View Controller and displaying another View Controller

  4. 4

    How to pass label data to another label in the second view controller

  5. 5

    Passing data from the second view into the first view controller

  6. 6

    Passing data from the second view into the first view controller

  7. 7

    Modal View controller not displaying at Center

  8. 8

    Modal View controller not displaying at Center

  9. 9

    Passing Data from a View Controller to a First Detail Controller and then to a Second Detail Controller

  10. 10

    Updating Label of a View Controller by a NSDictionary

  11. 11

    Moving from first view controller to second after 5 seconds

  12. 12

    NavigationBar not appearing in second view controller

  13. 13

    Second view controller has no data

  14. 14

    iOS 7 : How to change the status bar text color as white in one view controller and black in second view controller?

  15. 15

    Save text of label when viewing another view controller

  16. 16

    Second view not appearing within view controller

  17. 17

    cast text from tableview with 2 sections to second view controller

  18. 18

    Moving to a second view controller from one view controller programatically

  19. 19

    Moving to a second view controller from one view controller programatically

  20. 20

    ng-view is not displaying the pages but controller is working

  21. 21

    Assistant Editor not displaying correct view controller?

  22. 22

    Displaying an URL from a different view controller in a UIWebView

  23. 23

    Displaying the Initial View Controller as a Form Sheet

  24. 24

    Displaying an URL from a different view controller in a UIWebView

  25. 25

    NameError in Controller and View is only displaying html

  26. 26

    Displaying a partial view from another controller

  27. 27

    How to pass a value from second view to first view through tab bar controller

  28. 28

    How to run second view controller only when data from server is loaded in coredata from first view controller in swift

  29. 29

    How can I use text from a table view cell and display it as a label in a parent view controller?

HotTag

Archive