Create segue when tapped on textfield & pass data through navigation back button

This tutorial has been updated for X code 8.0 & swift 3.0

This is common when you have a form & you want to write a note/memo related to the form entry. Normal text field may be too small for contents of note. And at the same time you don’t want to show huge text view in already big form. This is where you can create segue when tapped on textfield and pass data back and forth.

1. First lets set up the x code project as usual. Create new single view application X code project. Set project name to textfield segue & save it on desktop.

new x code project

2. Go to main storyboard, select view controller and go to Editor -> Embed In -> Navigation Controller. Then set navigation item title to Form.

add navigation controller

3. Select view controller, go to bottom at main storyboard & set size to  iPhone SE

set-size-to-iphone-se

 

4. Add textfield in form view controller as follows and set its text to note as follows

add textfield

5. Add new view controller next to form view controller as follows

add new view controller

6. Create show segue between form view controller and new view controller as follows

create show segue

7. Select show segue and set its identifier to openNote as follows.

set segue identifier

8. Add navigation item to new view controller and set its title to Note as follows

add navigation item

9. Add text view in Note view controller and remove all the text inside of it as follows

add text view

10. Select the text view & pin all sides to view controller as follows

pin to view controller

11. Add new cocoa touch class file of subclass UIViewController as follows

add view controller swift file

12. Go to main storyboard, select note view controller and set its custom class as follows

set custom class

13. Create outlet for note text field to note view controller as follows

textfield as outlet

14. Add UITextFieldDelegate as subclass to view controller as follows

// 14
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var noteTextField: UITextField!

15. In view did load set delegate of note text field to view controller as follows

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // 15
    noteTextField.delegate = self
}

16. Now write method from textfield delegate and perform segue when someone taps on textfield as follows

    // 16
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        
        performSegue(withIdentifier: "openNote", sender: self)
        
        return false
    }

17. Go to main storyboard, select note view controller, open assistant editor and create outlet for text view as follows

text view as outlet

18. Open note view controller swift file and create note string variable as follows

@IBOutlet weak var noteTextView: UITextView!

//18
var note: String?

19. Open view controller swift file and write prepare for segue method as follows

    // 19
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        if segue.identifier == "openNote" {
            
            let noteController = segue.destination as! NoteViewController
            
            noteController.note = noteTextField.text
        }
    }

20. Go back to note view controller swift file and write following code in view did load

override func viewDidLoad() {
    super.viewDidLoad()

    // 20
    if let note = note {

        noteTextView.text = note
    }

    noteTextView.becomeFirstResponder()

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

21. Now build and run the app on iPhone 5s and you will see the text field data is moved to text view as follows

OGGfnPVoZY
Download X code file
Challenge : Pass the edited data with back button / form button only.

X5b6wlw9ND
Download Challenge Solution

Total 11 Votes
5

Tell us how can I improve this tutorial?

+ = Verify Human or Spambot ?