ViewDidAppear is not firing

mikeberlinmikeberlin USInsider, Beta, University, Developer Group Leader ✭✭

Hello,

I'm building a nib-less iOS app utilizing a UINavigationController. I do not want the home screen to show the NavigationBar, but on the sub pages I do want to show the NavigationBar.

My hope was to use the ViewDidAppear event to show/hide the NavigationBar as I push and pop UIViewControllers to the UINavigationController. However, ViewDidAppear does not appear to be firing for my UIViewControllers.

Here is my AppDelegate.cs:

    Window = new UIWindow (UIScreen.MainScreen.Bounds);

    UINavigationController rootNavController = new UINavigationController ();
    rootNavController.PushViewController (new Home (), false);

    Window.RootViewController = rootNavController;
    Window.BackgroundColor = UIColor.Black;
    Window.MakeKeyAndVisible ();

And here is my Home UIViewController:

    public partial class Home : UIViewController
    {
        #region Constructors

        public Home () : base()
        {
        }

        #endregion

        #region ViewController Events

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            // I setup UI elements here but removed for brevity.
        }

        public override void ViewDidAppear (bool animated)
        {
            base.ViewDidAppear (animated);

            this.NavigationController.SetNavigationBarHidden (true, false);
        }

        #endregion
    }

The button TouchUpInside method I use to get to the next UIViewController is this:

    btnFindCourses.TouchUpInside += (object sender, EventArgs e) => {
        this.NavigationController.PushViewController(new FindCourses(), true);
    };

Flow-wise everything works fine (i.e. the UIViewController FindCourses loads up and I see the UI elements that I add in the ViewDidLoad event (where I am also showing the NavigationBar). So on first load of the app, no NavigationBar on the home page and then the NavigationBar appears on the sub page (great!).

The trouble is, when I tap the Back button in the NavigationBar to go back to the Home UIViewController the NavigationBar is visible, even though I have the code above in the Home ViewController ViewDidAppear event which is supposed to hide the NavigationBar.

I've setup breakpoints in the ViewDidAppear method and it never hits. Any suggestions on what's going on? Let me know if I can provide any more code. I have also tried ViewWillAppear to no avail.

Thanks everyone!

Posts

  • TommyBaggettTommyBaggett USUniversity ✭✭✭
    edited August 2013

    Hi @mikeberlin, I'm not sure why the ViewDidAppear wouldn't be firing for you, but I have the same need to hide/show the nav bar in my app. Here's how I did it.

    Initial controller with no nav bar:

        override public void ViewWillAppear (bool animated)
        {
            base.ViewWillAppear (animated);
            var window = ServiceContainer.Resolve<UIWindow> ();
            (window.RootViewController as UINavigationController).SetNavigationBarHidden (true, true);
            (window.RootViewController as UINavigationController).SetToolbarHidden (true, true);
        }
    

    Second controller with nav bar visible:

        override public void ViewWillAppear (bool animated)
        {
            var window = ServiceContainer.Resolve<UIWindow> ();
            (window.RootViewController as UINavigationController).SetNavigationBarHidden (false, true);
        }
    

    The ServiceContainer class was ripped from the Xamarin Field Service app. This was some early stuff I did when I was first learning Xamarin, so it may not be necessary to access window.RootViewController.

    Hope this helps!

  • mikeberlinmikeberlin USInsider, Beta, University, Developer Group Leader ✭✭

    Thanks for the reply @tommybaggett, but ViewWillAppear does not seem to fire for me either.

  • TommyBaggettTommyBaggett USUniversity ✭✭✭

    Sorry, I missed that in your initial post. I assume other breakpoints are working for you, just not the ones in View Will/Did Appear? Perhaps try some Console.Writeline output to verify they're not being called?

    I don't want to be Captain Obvious haha, but I haven't seen a case of those events not firing, so I'm thinking of sanity checks to clear obvious issues out of consideration. The only difference in the code you provided and my setup is that I'm loading the navigation controller and first view controller from a storyboard.

  • AndrewReedAndrewReed USMember ✭✭

    Mike, i think there must be an issue with the way your loading your views, as this would explain why your viewwill and did appear are not being called.
    Maybe its related to your base() not having an intptr so its not calling all of the lifecycle methods for a view.

  • mikeberlinmikeberlin USInsider, Beta, University, Developer Group Leader ✭✭

    Thanks for the replies @tommybaggett and @andrewreed. I'm a bit embarrassed but I can't explain it. I shut down my machine at work before I left on Friday and it wasn't working. I turned on my machine today to try again and it's working as it should have been (no change of code).

    Could have something to do with the fact that I'm using the alpha build? Not sure, but I'm glad it's working now.

    Thanks again for your replies!

  • AndrewReedAndrewReed USMember ✭✭

    Sounds like the changes didn't sync with xamarin studio, my suggestion is to close the solution and reload it next time. Sometimes xamarin studio has a mind of its own.

  • TommyBaggettTommyBaggett USUniversity ✭✭✭

    No problem @mikeberlin. I'm glad it worked out that easily for you in the end. If only a restart would fix all the problems, haha.

  • jmendesjmendes PTMember

    Hello all, a bit too late but I had the same issue and if you go to Run -> Clean All and then run it again the problem will be solved
    Better then restarting :)

  • SergeyMorenkoSergeyMorenko RUMember
    edited August 2014

    the same problem on iOS8 beta 5. All works perfect on simulator but doesn't work on real device. Xamarin team, could you please check. Please note: ViewWillAppear doesn't work too

  • MuneebSaeedMuneebSaeed USMember ✭✭

    Hey Everyone. I'm facing the same issue with iOS 13. I had done many things to solve this but no gain. Should i update my mvvmcross packages? Currently i'm using mvvmcross 4.2.3. I tired to update but due to many projects in my solution, somewhere assemblies could not link. Please help me out.

Sign In or Register to comment.