Navigate to viewcontroller from AppDelegate

MortenGryning.1061MortenGryning.1061 DKMember ✭✭
edited March 2016 in Xamarin.iOS

When my app is running in the background and the app receive a notificaionthe the user can click on the notification. Doing this calls DidReceiveRemoteNotification(...) in AppDelegate.

In DidReceiveRemoteNotification I would like to take the user to a specific viewcontroller. I have tryed several ways but not found something that works.

Method1
UIStoryboard board = UIStoryboard.FromName("Main", null);
SpecificCtrl ctrl = (SpecificCtrl)board.InstantiateViewController("SpecificCtrl");
UINavigationController nav = new UINavigationController(ctrl);
Window.RootViewController.PresentViewController(nav, true, null);

Result: The viewcontroller is loaded but the navigationbar and toolbar are empty. Also there is no way to go back.

Method2
UIStoryboard board = UIStoryboard.FromName("Main", null);
SpecificCtrl ctrl = (SpecificCtrl)board.InstantiateViewController("SpecificCtrl");
Window.RootViewController.PresentViewController(ctrl , true, null);<del

Result: The viewcontroller is loaded but the navigationbar and toolbar are not present

Question
Do you have any suggestions? The storyboard is found, the specificctrl have storyboard id specified and the navigation actually work. But somehow the navigationbar and toolbar are not loaded properly

Answers

  • IsaacCorralesRuizIsaacCorralesRuiz MXMember ✭✭
    UIStoryboard board = UIStoryboard.FromName("Main", null);   
    SpecificCtrl ctrl = (SpecificCtrl)board.InstantiateViewController("SpecificCtrl");
    UINavigationController nav = new UINavigationController(ctrl);
    Window.RootViewController.PresentViewController(nav, true, null);
    

    Result: The viewcontroller is loaded but the navigationbar and toolbar are empty. Also there is no way to go back.

    you are creating a new UINavigationController therefore there are no controllers except for the one you are passing it as a Root which is ctlr in your case.

    Suggestions:
    Use a UINavigationController as your windows.RootviewController and use PushViewController method instead of PushViewController

  • MortenGryning.1061MortenGryning.1061 DKMember ✭✭

    Hi Isaac

    Thank you for your answer. I'm not sure I follow though.

    Should I create a new UINavigationController in finished loading? Or where do I obtain the existing UIViewController from?

    Also that line: "use PushViewController method instead of PushViewController" doesn't make sense to me. Could you elaborate?

    Best regards
    Morten

  • IsaacCorralesRuizIsaacCorralesRuiz MXMember ✭✭

    A UINavigationController is a subclass of UIViewController that manages a stack of view controllers, the navigation bar is only visible for UIViewController inside this stack.

    PresentViewController is a method of the UIViewController class you use to present a view controller. In your case it is showing a UINavigationController with a root which is your storyboard, it wont show any back button on your navgation bar because there's only one ViewController in the stack.

    PushViewController is a method of the UINavigationController that add ViewControllers to the stack and therefore it allows the navigation between them, adding the back button the first time you call it.

    What you should to is something like this:

        UINavigationController navController;
        public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
        {
            navController = new UINavigationController(myMainController);
            Window = new UIWindow(UIScreen.MainScreen.Bounds);
            Window.RootViewController = navController;
            Window.MakeKeyAndVisible ();
    
            return true;
        }
    
    
    
        public override void DidReceiveRemoteNotification (UIApplication application, NSDictionary userInfo,                                            System.Action<UIBackgroundFetchResult> completionHandler)
        {
            navController.PushViewController (myOtheController,true);
    
        }
    
  • MortenGryning.1061MortenGryning.1061 DKMember ✭✭

    Sorry for the late answer.

    I'm still struggling with this. What would myMainController be in your example? I have tried with Window.RootViewController. I use a storyboard and are not sure how the storyboard and the custom initiated navigationcontroller interacts.

  • IsaacCorralesRuizIsaacCorralesRuiz MXMember ✭✭

    As I mentioned before a UINavigationViewController manages a stack of view controllers, when you create a new navigationController, it will be empty unless you specify a root to it. navigationController.RootViewController is different from Windows.RootViewController. myMainViewcontroller in my example was a representation of the first view that should appear in my application. You also can have as many UINavigationController as you want.

    In order for you to show a viewController and then be allowed to back to anotherViewController using a NavigationController, both viewControllers should be on your stack inside the same navigationController.

    Window.RootViewController assigns the first viewController what should be shown when your application starts,
    if you assign a viewController as a Windows.RootViewController you wont have any NavigtionToolbar and the only way to show any other View will be calling PresentViewController. For this reason I'm assigning a navigationController as a
    Window.RootViewController, so later I can push or pop views, having access to the navigationToolbar.

    I'm using a UINavigationController as field, for the app delegate because doing it I have access to it when DidReceiveRemoteNotification is called, after that I can push a viewController, at this point I will have at least 2 controllers in the stack, my mainViewController and the viewController I decided to push in this method. Now you will be able to go back to your mainViewController because both of them are in the stack , the toolbar will also display a back button.

    Now I'll explain what were you doing wrong on your methods:

    Method1
    UIStoryboard board = UIStoryboard.FromName("Main", null);
    SpecificCtrl ctrl = (SpecificCtrl)board.InstantiateViewController("SpecificCtrl");
    UINavigationController nav = new UINavigationController(ctrl);
    Window.RootViewController.PresentViewController(nav, true, null);

    Here you created a totally new UINavigationController and then you added ctrl as a root, since there's no other controller inside that navigationController, no back button will be shown. Here Window.RootViewController.PresentViewController(nav, true, null); you are just telling your rootviewcontroller to paste another viewcontroller in front of it, your root viewconroller is not part of the stack of viewControllers from you new navigationController.

    Method2
    UIStoryboard board = UIStoryboard.FromName("Main", null);
    SpecificCtrl ctrl = (SpecificCtrl)board.InstantiateViewController("SpecificCtrl");
    Window.RootViewController.PresentViewController(ctrl , true, null);

    Here you don't have any navigationController therefore you won't have any navigation Toolbar either

  • divyagarimelladivyagarimella Member ✭✭

    hi all
    after calling this viewcontroller how to call LoadApplication(new App()); in xamarin forms ios

Sign In or Register to comment.