-1

I'm working on project that has multiple view controllers, one of the view controllers has many text fields and a button which when you click it takes u to another view controller which is has map view. When a user selects a place on the map and afterwards tries to go back to the first view controller the previously populated textfields are empty.

How can i go back to the origin view controller and find the textfields and picker view still retaining the same values which were input initially before the segue.

here how i segue to second VC

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toOnlyMap" {
        let distenation = segue.destination as? onlyMapVC
        distenation?.latitude = latitude
        distenation?.longitude = longitude
    }
}

@IBAction func toMapClicked(_ sender: UIButton) {
    performSegue(withIdentifier: "toOnlyMap", sender: self)
}

here how i unwind the second VC

@IBAction func unwindToAdd(_ sender:UIStoryboardSegue) {

   if latitude == 0.0 || longitude == 0.0 {
      Helper.showAlert("Error", message: "you did not choose any place click back to dismiss this page", VC: self)
   } else {
      performSegue(withIdentifier: "backFromMap", sender: self)
   }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "backFromMap" {
        let destination = segue.destination as? addNotificationVC
        destination?.latitude = latitude
        destination?.longitude = longitude
        destination?.color = "Ggreen"
        destination?.theHint = "you have choose the location"
    }
}
maxwell
  • 3,788
  • 6
  • 26
  • 40
alfhd
  • 35
  • 6
  • 3
    Please add your code and delete the pictures. – maxwell Oct 05 '18 at 12:21
  • how to post the code ? – alfhd Oct 05 '18 at 12:30
  • Click the button "{ }". After that you will see: "enter code here". – maxwell Oct 05 '18 at 12:32
  • i did your suggestion – alfhd Oct 05 '18 at 12:38
  • Is the `backFromMap` segue an *unwind segue*? I.e. did you create it by dragging to the "exit" icon of your map scene in the storyboard? – Paulw11 Oct 05 '18 at 13:08
  • no Paul i made this by dragging from the VC itself to the first VC not from exit – alfhd Oct 05 '18 at 14:40
  • I just posted an answer that should help you to properly set up and use an unwind segue. In particular look at the link to another question I linked to for how to create an unwind segue in a StoryBoard. Then look at how I used `isMovingFromParentViewController` in `viewWillDisappear`. –  Oct 05 '18 at 17:44

2 Answers2

0

The problem is that what you call an “unwind” segue is not in fact an unwind. You are making a whole new first VC, piling it on top of the existing second VC and the existing first VC. That will cause many issues, such as memory waste. The new VC thus starts life with empty text fields (because it is new); but that is just a symptom of a far deeper problem! Meanwhile the old VC is still sitting there with its “populated textfields“, hidden behind not one but two other view controllers and their views.

You can easily confirm this by performing your false “unwind” and then looking at the View Debugger. You will actually see two copies of the first VC in the hierarchy.

Another proof would be to log on viewDidLoad in the first VC. If your "unwind" segue causes viewDidLoad to be called, it is not an unwind segue — you're creating a new first VC.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • ok sir can u plz help how to unwind without creating a new VC ? – alfhd Oct 05 '18 at 14:28
  • Yes, I can, but I don't have to because it has been discussed many times here. See for example https://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them My role is to answer your _question_, which is, why are my text fields losing their populated values? It's because they are not the text fields you populated; they are a new, different set of text fields. – matt Oct 05 '18 at 15:14
-1

It's great to see new people getting into the field. I suggest you read up on something called "MVC" which stands for Model-View-Controller. The problem you are having exists because you don't have any models. You only have a bunch of views and a couple of controllers.

What do these "many text fields" represent? Is it information about a user, or is it camera meta-data or something else entirely? Create a model in your view controller and fill it out as the user enters information.

You seem to have a model of sorts in your second view controller: latitude and longitude but nothing in the first VC.

Daniel T.
  • 32,821
  • 6
  • 50
  • 72