1

I just recently started to use Swift and am facing a "weird" bug with the viewDidLoad method.

My very simple app currently only has 2 viewcontrollers:

  • MainViewController, which is the Apps entry point and serves as an overview for the data which has already been created. It also provides the option to add new data, which would trigger a segue to
  • DataViewController, which provides the UI to create new data, after which it goes back to the MainViewController

Now my issue is that the viewDidLoad method of MainViewController is always called whenever the MainViewController appears (At the start of the app and every time the DataViewController disappears). Particularly, the msg "MainViewController newly created" is always printed.

Even worse, it seems that my app is "secretly" resetting. To show this I have defined the class variable "createView" in my MainViewController which is true by default, and is set to false during the viewDidLoad (the only place where this variable is called/set). However the msg "MVC newly created" is still always printed in the output after the MainViewController shows up. How can that be? Why / how is createView reset to true?

Hope this snippet is enough to find the issue. Otherwise, let me know if something is missing.

Thanks for your help!

override func viewDidLoad() 
{
    super.viewDidLoad()


    if (createView)
    {
        determineArraySize()
        createDataArray()
        print("MainViewController newly created")
        createView = false

    }
    else {print("Nothing happened")}
}
Kai
  • 13
  • 4
  • 3
    What kind of segue is connecting the DataViewController to the MainViewController? My guess is that you're actually pushing a new instance onto a navigation stack of sorts.. thus recreating the MainViewController every time. – moritz Apr 18 '17 at 22:44
  • Provide the code where you actually wrote the code for _disappearing_ your `DataViewController`. – nayem Apr 19 '17 at 03:36
  • @moritz: I use the "Show(e.g. push)" segues (just due to the reason it is the top option to pick when creating a segue in the storyboard). However, I have changed the type to "Show Detail" and "Present modally". Didnt have an effect, neither in the output, which still indicates a call of viewDidLoad, nor a visual effect (am I doing sth wrong?) – Kai Apr 19 '17 at 22:54

1 Answers1

1

As @moritz mentioned in the comments, check out the way you present the DataViewController in your storyboard.

If the view is presented modally, you should call:

dismiss(animated: true, completion: nil)

If the view is presented using a show seque, you should call:

_ = navigationController?.popViewControllerAnimated(true)

the_legend_27
  • 571
  • 4
  • 11
  • thanks for your reply. As I have added as reply to Moritz comment, I use the "Show (e.g. push)" segue kind. To be honest, I don't really understand what and where I should paste your mentioned LoC. Is there a good guide I could use? The only resource I could find is the official one from Apple, which does not help me much. Thanks – Kai Apr 19 '17 at 22:58
  • i think the issue is that you're using a segue to go from dataViewController back to the mainViewController.. this makes a new mainViewController instead of going back to the previous VC. Instead, try adding this code to a button touch event in DataViewController: navigationController?.popViewControllerAnimated(true) ....for tutorial, watch this video https://www.youtube.com/watch?v=VhXhqX3NeIY – the_legend_27 Apr 20 '17 at 01:53
  • Thanks for sharing the video and the info. I basically understood the problem and was able to use the code you provided. 1 thing however is unclear to me -- you wrote I should use "dismiss..." if the view is presented modally. However I found that I need to use this although I have a "Show" Segue. Could it be because I did not use the navigation stack controller but instead created two separate views with manually added nav bars & bar buttons (to have the option to use different bar buttons in my 2nd/DataVC) – Kai Apr 20 '17 at 22:28
  • yup, exactly! you cant use the navigationController func unless you create a navigation stack controller for your mainviewcontroller in the storyboard, and show a dataviewcontroller using a push segue. In this scenario, the dismiss func will not work. But as you mentioned that both views nav bar are created manually, the navigationController func will not work and dismiss func should do the trick. if you want to customize the nav bar, you can try following this: https://www.ioscreator.com/tutorials/customizing-navigation-bar-ios8-swift – the_legend_27 Apr 21 '17 at 02:26
  • Thank you very much for your help! – Kai Apr 24 '17 at 22:54