ViewDidAppear firing in IOS Simulator but not on Device

matt1701matt1701 USMember
edited November 2013 in Xamarin.iOS

Hi
I have created a Home screen that has a button on that displays a Game screen when it is pushed. My AppDelegate declares a Navigation controler and wires up the HomeScreen to the NavigationController using the code below.

var rootNavigationController = new UINavigationController ();

HomeScreen homeScreen = new HomeScreen ();

rootNavigationController.PushViewController (homeScreen, false);

this.window.RootViewController = rootNavigationController;

In the Home screen , I have code like this to display the Game Screen.

if(this.gameScreen == null)
{
this.gameScreen = new GameScreen();
}
this.NavigationController.PushViewController (this.gameScreen , true);

I have some initialisation code in the Game Screen and I do it in the ViewDidAppear override function. So, anytime I am moving from the Home Screen to the Game screen I would expect the ViewDidAppear function to be called. This seems to work fine in the simulator (I can debug and see the code jump into the ViewDidAppear function every time I move from the Home to Game Screen). BUT, on the actual IOS device, this ViewDidAppear code is not called when I go back into the Game screen.

Just wondering if I have missed something here that is not allowing the ViewDidAppear code to fire on the actual device? Also, why is this behaviour different on the device compared to the Simulator?

Thanks for any help

Posts

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    I don't see any obvious reasons why your code is not working, but I do see some things that I normally don't do. You should keep a class level reference of your rootNavigationController in your AppDelegate.cs so it does not get GC'd. You might consider changing your code to look more like this to prevent strange GC issues.

    // Class Level Declaration
    HomeScreen homeScreen;
    UINavigationController rootNav;
    
    
    // Inside FinishedLaunching
    homeScreen = new HomeScreen();
    rootNav = new UINavigationController(homeScreen);
    window.RootViewController = rootNav;
    

    Also, why is this behaviour different on the device compared to the Simulator?

    There are a number of reasons for this. Some of them being: The compilation is different, AOT (device) vs JIT (simulator) and the simulator gets full access to the resources (CPU, RAM, etc) of your computer while the device is limited to its own resources. This means memory problems occur differently, and GC can happen more often (I think).

  • matt1701matt1701 USMember
    edited November 2013

    Hi John
    Thanks for your suggestions. I tried making the changes you said but the ViewDidAppear events are still not firing. As regards my code, I followed the tutorial example found here
    http://docs.xamarin.com/guides/ios/getting_started/hello,_mvc/

    I did a search on Google regarding this issue and there seems to be some talk of this with examples on how to do resolve this using Objective-C but as I am new to all this I'm not really sure how to translate those suggestions into mono touch and c#.

    I'd really appreciate it if you could maybe try a quick test and see if you are getting the same issues? Remember it all works fine in debug mode on the Simulator but not when released on the actual IOS device (an iPhone 4S)

    Many thanks

  • DannyCDannyC US ✭✭✭

    @matt1701 your code is always executed faster on the Simulator since it runs of the Mac's resources as @John mentioned. Wondering if you code is taking to many long on the device and getting killed by iOS? Try commenting the code on your ViewDidAppear and do a simple Console.WriteLine("ViewDidAppear fired");. If that works then the event is getting fired properly and just a matter of optimizing your code.

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    If you can attach a sample project, ill take a look at it and see if I can reproduce it. ViewDidAppear works fine for me in all my projects.

  • matt1701matt1701 USMember

    OK, this is weird. I added the Monotouch.Dialog framework into the references and all is working fine on the Device now!!!! 'm assuming that some essential references were not set up when using a blank project which somehow were included when I then added the Monotouch.Dialog reference?
    Thanks all for your suggestions anyway.

  • SimonBonannoSimonBonanno MTMember ✭✭

    Hi,

    I know this is an old thread, but i encountered something like this.
    I was getting the viewDidDisapper on the simulator, but on the device it was not firing. All i had to do was clean and re build the solutions. Hope this helps others.

  • NicholasTurnerNicholasTurner USMember, University ✭✭
    edited October 2015

    I would say you have a BIG issue in your intialization code. Plus what code is needed each time? When you pop home controller the view disappears but is still in ram and if you fire that again, unless your clearing ram on view will disappear you will eventually crash the system.

    ViewDidAppear only fires when a view appears, so if your doing something like hiding by alpha or moving off screen it never disappeared

    I would say you were block the thread with an exception or some wait condition.

    Typically if you see this iOS instruments is a great tool to figure this out and COnsole.WriteLine is your friend, use it judiciouly when you can see what is going on.

Sign In or Register to comment.