1

My app has this relationship as the screenshot shows here screenshot. What I am trying to do is after clicking the button share in the 4th view, I want remember my info from the 4th page (like comments, location...) and then return to the default page of the tab bar navigator. I achieved this by segue (present modally) right now, but I realize that in this way the memory is continuously consuming. So, I am wondering how I can achieve this elegantly?

Burning
  • 37
  • 3

2 Answers2

1

Look up unwind segues!

What you should do is to create an unwind segue instead of a normal push segue from the 4th VC to the first VC. You basically have to write this method in your first VC:

@IBAction func unwindFrom4thVC(_ segue: UIStoryboardSegue) {

}

Then connect the 4th VC with the "Exit" of the first VC, then select the above method in the pop up that appears.

The unwind segue won't show up in the storyboard but you can find in the document outline. Select it, give it an identifier, and perform it using:

performSegue(withIdentifier: "your identifier", sender: yourData)

where yourData is the data that you want to remember.

Now in prepare(for:sender:), you can do this:

if let vc = segue.destination as? YourFirstVC {
   vc.data = sender as? YourDataType
}

data is a property you need to declare to receive the data from the 4th VC, and YourDataType is the type of that data.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

Use NotificationCenter like this.

Registe a function with this addObserver in tabBarViewController

let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(self, selector: #selector(YourClass.sayHello), name: "TestNotification", object: nil)

Use this method to share view controller

NotificationCenter.default.post(name: NSNotification.Name("TestNotification"), object: YourObjectLikeDictOrArray)

In tabBarViewController sayHello method

let userInfo = notification.userInfo
let myObject = userInfo["someKey"] as? Sometype
Mathi Arasan
  • 869
  • 2
  • 10
  • 32