Unable to Push Controller

Jeff_HJeff_H Member ✭✭

I'm a junior programmer fresh out of school, and just started working on my first Xamarin.iOS app. Things have been moving along fairly well until this issue came up. I am trying to to change view controllers from within another view controller.

I am not using storyboards, I am loading custom XIBs programatically.

Here's a snip from my AppDelegate which fires the first controller

window = new UIWindow(UIScreen.MainScreen.Bounds); appStartUpController = new AppStartUpController(); window.RootViewController = appStartUpController; window.MakeKeyAndVisible();

This works fine, and fires the appStartUpController. From here, I do some background work grabbing some data from an API call. I show a loading screen until the call is done and then attempt to load another view controller like so:

var controller = new CityPickerViewController(); UINavigationController navCont = new UINavigationController(); navCont.PushViewController(controller, false);

No exceptions are fired, and the next view will not load. Within the CityPickerViewController I placed break points on ViewWillAppear(), and ViewDidLoad() but they never get hit. I'm sure I'm missing something basic here, but have not been able to determine what that is. Any help would be appreciated.

Best Answer

  • LandLuLandLu Xamurai
    Accepted Answer

    You should use a UINavigationController to wrap your controllers like:

    window = new UIWindow(UIScreen.MainScreen.Bounds); 
    appStartUpController = new AppStartUpController();
    UINavigationController navCont = new UINavigationController(appStartUpController);
    window.RootViewController = navCont; 
    window.MakeKeyAndVisible();
    

    Then when you want to navigate to a new controller from appStartUpController try:

    var controller = new CityPickerViewController();
    this.NavigationController.PushViewController(controller, true);
    

    The error of your code is you only create a new UINavigationController but you haven't placed it into the current window stack. So nothing happens whe you call navCont.PushViewController(controller, false);.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    You should use a UINavigationController to wrap your controllers like:

    window = new UIWindow(UIScreen.MainScreen.Bounds); 
    appStartUpController = new AppStartUpController();
    UINavigationController navCont = new UINavigationController(appStartUpController);
    window.RootViewController = navCont; 
    window.MakeKeyAndVisible();
    

    Then when you want to navigate to a new controller from appStartUpController try:

    var controller = new CityPickerViewController();
    this.NavigationController.PushViewController(controller, true);
    

    The error of your code is you only create a new UINavigationController but you haven't placed it into the current window stack. So nothing happens whe you call navCont.PushViewController(controller, false);.

  • Jeff_HJeff_H Member ✭✭
    edited October 4

    That did the trick, thank you very much. Placing the controller into the Navigation Controller creates a "back" dialog at the top of the screen in the new view. I'm not looking to allow users to navigate back from this view, as that simply sends them back to the loading screen which is of no use. For activities where backward navigation is not required, should I be using ShowViewController(controller, this); instead or just hide the navigation bar by calling NavigationController.SetNavigationBarHidden(true, false); ?

Sign In or Register to comment.