How to segue to previous screen using Swift without losing data in swift 4

swift unwind segue
pass data back to previous view controller swift 4
unwind segue programmatically
pass data between view controllers swift without segue
dismiss view controller swift
passing data between view controllers using delegates in swift 4
passing data from tableview to viewcontroller swift 4
how to use segue in swift 4

I have a button from my create account page that opens up another view controller that has some information in it. i move it by doing a segue:

self.performSegue(withIdentifier: "sugueToRules", sender: self)

on my other Viewcontroller i have a back button that connects form the storyboard, to the create account one (the precious one)

when i go back i lose all my data that i put on the create account, so i must open a new instance of it.

how do i keep all my data of it when i go back ? do i have to save a local file somewhere and load it when i come back ?

i'm using swift 4

Make sure that they are managed by a UINavigationController. Then the back button will do its job just fine (no need for you to 'wire' something -- this is the default behaviour). Right now it appears that you are allocating a new controller each time you go back.

The same applies to a modally presented controller (with a custom close button for example), although you have to dismiss (or use an exit segue) instead of firing up a segue pointing back.

Unwind Segues Step-by-Step (and 4 Reasons to Use Them), Unwind segues are not limited to the last transition. For example, when you have several modal view controllers on screen at a specific time, For simplicity, I used the view controller as the data source, but this is not a good practice. We now want to go back to the previous screen when the user tapsĀ  Today, we go over how to pass data back and forth using segues and unwind in Swift 4. We will go over how to connect the segue, access the controller the data will be passed to, and how to receive

Passing segue to previous screen will creates a new instance of the previous viewcontroller. It it better to be bound in navigation controller and popout the presented screen using when back navigation is not required.

self.navigationController?.popViewController(animated: true)
self.dismiss(animated: true, completion: nil)

How To Pass Data Back To Previous View Controller Like A , how to pass data back to a previous view controller using a protocol, Data Back To Duration: 18:16 Posted: Dec 24, 2018 By clicking on that I take to another screen where user can choose an option and I want it back in the previous view controller when back button is pressed. I can put a custom bar button item, but I want to return to the parent view controller using the back button in the navigation bar rather than with a custom button on the view.

you need to use UINavigationController for navigating UIViewController like -

Swift. Pass data back to a previous view controller, In this video I am going to share with you how to send data back to the previous view Duration: 17:27 Posted: Jan 16, 2016 In today's Swift Tutorial I'll show you how to use an unwind segue to go back to a previous screen using storyboards in Xcode. I'll also show you how to pass data between two views via the unwind

Showing and Hiding View Controllers, Display view controllers using different techniques, and pass data between them Using segues in your storyboard is the recommended way to present and Calling the dismiss(animated:completion:) method might not always be the most your view controller, for example when restoring your app's UI to a previous state. Tutorial on how to pass data between view controllers using a segue. Source Code: https://github.com/awseeley/Swift-passing-data-using-segues-tutorial Be sur

3 ways to pass data between view controllers (forth and back), The prepare(for segue:, sender:) method will be called by the view controller just before a So now you want to pass a data to the previous view controller DisplayViewController.swift protocol DisplayViewControllerDelegate or pushing view controller programmatically without using storyboard segue. Since you're using same variable across the two Viewcontrollers, namely currentUser (type Bool). So its better to make it a global variable in both classes. When coming to global variable concept in swift. Everything by default in swift is public, and thus if you declare something like this:

Move between View Controllers with Segues, Creating new View Controllers (aka screens) in an app and moving Add a new View Controller to the storyboard; Add a new Swift file for a new function, remembering to make it an Action and not an Outlet: Ok, our second View Controller is now ready to accept and display some data from the first. Same goes for segues when you segue to something embedded, the destination view controller will be the embedding controller, not the controller you're usually interested in. Anyhow, you should be able to fix the problem in a similar way you've done in Objective-C, so this is just an exercise in syntax porting.

Comments
  • Make sure that they are managed by a UINavigationController. Then the back button will do its job just fine. Right now it appears that you are allocating a new controller each time you go back. The same applies to a modally presented controller, although you have to dismiss (or use an exit segue) instead of firing up a segue pointing back. I hope that this makes sense.
  • @Alladinian great answer, put it as an answer and i'll accept it
  • @ChiefMadog Done. Glad that helped you :)