13

In my iOS app, I have a UITabBarController, and its viewControllers list looks like [vc1, vc2], which are of class MyVC1 and MyVC2 respectively, both of which subclass UIViewController. MyVC1 overrides viewWillAppear, and the overwritten version prints something so I know when it is called. (This is to isolate a larger problem I had, which was a view not appearing.)

My issue is that when the app first starts up and vc1 is the selected tab, nothing is printed, meaning its viewWillAppear is not being called. If I switch tabs and then go back to vc1, something does get printed, so vc1's viewWillAppear is not being called until I switch back to it from another tab.

Is there any way to have vc1 call viewWillAppear as soon as the app starts up, without needing to switch tabs? Honestly, I'm surprised this wasn't the default behavior already.

4 Answers 4

21

I just made a new test app in Xcode with nothing but a UITabViewController (and its two child view controllers) in Main.storyboard, and when I override viewWillAppear for the first child, my "view will appear" log prints every time (including on app launch).

Here are a couple things that could cause viewWillAppear to not be called:

  1. Not calling super somewhere you've overridden viewWillAppear
  2. Adding a view controller's view somewhere as a subview without adding the view controller as a child view controller

You also might try seeing if viewWillAppear is getting called for your UITabBarController, and if not, is it being called for its parent or presenting view controller? And so on until you find where the holdup is.

2
  • 3
    Thanks, I overrode the viewWillAppear of UITabBarController without calling super.viewWillAppear inside. Stupid mistake... Sep 12, 2016 at 1:50
  • 1
    I Appreciate this answer. It's a very small thing and normally we miss it. It saved a lot of time. Thanks Sep 18, 2021 at 11:05
4

if you have Custom UITabBarController, check override func viewWillAppear is call super

in Custom UITabBarController

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated) // this line need
    }
1
  • oh,... i see comment.
    – sd0
    Feb 21, 2019 at 8:57
1

Use the following line in viewWillAppear method of your vc1:

  [super viewWillAppear:animated];

Next, the UITabBarController must be your initial controller.

4
  • Sorry, that didn't help. It's still not getting called until I switch to vc1 from another tab. Sep 10, 2016 at 5:42
  • Can u post a screenshot of your storyboard?
    – KSR
    Sep 10, 2016 at 10:07
  • 2
    Thanks for your help. The missing call to super was in my UITabBarController. Sep 12, 2016 at 1:51
  • What do I do if UITabBarController isn't my initial controller? Nov 17, 2020 at 19:00
0

make shure in the UITabBarController in the viewwillapear have the super.viewWillAppear(animated) like the following code

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.