Search code examples
swiftuiviewcontrolleruinavigationcontrollerios9

Navigation between view controllers


On my application, I have four view controllers (VC):

  • HOME
  • MESSAGES
  • PROFIL
  • SETTINGS

On each VC, an opaque bar bottom with four bar button items for each VC (home, messages, profil, settings).

When I tap on one bar button item, I manage the navigation with a segue (show) and it loads the selected VC.

I would like to find a way to not load again a VC already loaded one time. For example:

  • The app is launched
  • HOME VC is loaded and appears
  • User taps on PROFIL
  • PROFIL VC is loaded and appears
  • User taps on HOME
  • HOME VC appears without calling viewDidLoad method, because it has already been loaded four steps ago

I tried with all types of segues (show, present modally, etc.) and with presentViewController method, but each time the viewDidLoad method is called.

Is there an easy way to do this or do I have to manage this with a boolean to indicate that the view was already loaded?

I would like a menu like WhatsApp application has for example.


Solution

  • WhatsApp uses a Tab Bar at the bottom. Are you using Toolbar? ViewDidLoad only executes once per view unless you unload the view from memory.

    The easiest way to adapt your app is to just add a Tab Bar Controller in your app and then delete the 2 automatically added view controllers and set a relationship (just like a segue; it's below segue types) between each view controller and the tab bar controller. To change the icons, modify them in the Tab Bar Controller View. Then segues will work automagically just like in WhatsApp. :)