Forum Xamarin.iOS

Unable to pop the VIEWCONTROLLer

AmejackAmejack USMember ✭✭✭
Whenever I get push notifications able to navigate.. But if I pop the viewcontroller unable to go back.. It stays in the same control or I will get the exception as(Tried To Pop To A View Contoller That Doesn't Exits)
Tagged:

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    How did you navigate to a new controller when the notification is triggered?
    There're two ways of displaying a new controller. If we use UINavigationController to push then the corresponding to return is poping. Or we need to use dismissing to go back to the previous controller.
    Try to show some code here to help us know more about your situation.

  • AmejackAmejack USMember ✭✭✭

    var nav = Window.RootViewController as UINavigationController;
    var storyboard = UIStoryboard.FromName("MainStoryboard", null);
    var ViewController = storyboard.InstantiateViewController("EmailDetailVC") as EmailDetailVC;
    // ViewController.storevalues = storevalues;
    var Nav = new UINavigationController(ViewController);
    nav.NavigationBar.Hidden = true;
    Window.RootViewController = Nav;

    this my code which i writren to navigte

  • LandLuLandLu Member, Xamarin Team Xamurai

    If the existed rootview controller is a UINavigationController you could use it directly to navigate to your new view controller so that the navigation stack could contain the previous view controllers.
    Modify it like:

    var nav = Window.RootViewController as UINavigationController;
    var storyboard = UIStoryboard.FromName("MainStoryboard", null);
    var ViewController = storyboard.InstantiateViewController("EmailDetailVC") as EmailDetailVC;
    
    nav.PushViewController(ViewController, false);
    
  • AmejackAmejack USMember ✭✭✭

    its stays in the same controller .if i pop the view controller

  • LandLuLandLu Member, Xamarin Team Xamurai

    How did you pop the current view controller?
    Try to debug the view controllers on the EmailDetailVC like:

    var controllers = NavigationController.ViewControllers;
    

    If it only contains one view controllers in the navigation stack, it means current UIViewController is the root view controller and we can't pop further.

  • AmejackAmejack USMember ✭✭✭

    ****# this is the code i have return for remote notification. from here directly i m navigating to email detail vc ..**

    **

         public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
                {
                    ProcessNotification(userInfo, false, application);
                }
    
                void ProcessNotification(NSDictionary options, bool fromFinishedLaunching, UIApplication application)
                {
                    // Check to see if the dictionary has the aps key.  This is the notification payload you would have sent
                    if (null != options && options.ContainsKey(new NSString("aps")))
                    {
                        //Get the aps dictionary
                        NSDictionary aps = options.ObjectForKey(new NSString("aps")) as NSDictionary;
    
                        string alert = string.Empty;
    
    
                        if (aps.ContainsKey(new NSString("alert")))
                            alert = (aps[new NSString("alert")] as NSString).ToString();
                        storevalues.SetBool(true, "pushnotification");
                        if (alert.ToString() == "e")
                        {
    
                            DispatchQueue.MainQueue.DispatchAsync(() =>
                            {
                                if (application.ApplicationState == UIApplicationState.Inactive || application.ApplicationState == UIApplicationState.Background)
                                {
    
    
                                    var nav = Window.RootViewController as UINavigationController;
                                    var storyboard = UIStoryboard.FromName("MainStoryboard", null);
                                    var ViewController = storyboard.InstantiateViewController("EmailDetailVC") as EmailDetailVC;
    
                                    nav.PushViewController(ViewController, false);
                                }
                            });
                        }
    

    this the code for poping the contoller

         public void EmailDetailBackButtonAction(object sender, EventArgs e)
                {
    
    
                            var storyboard = UIStoryboard.FromName("MainStoryboard", null);
                            var ViewController = storyboard.InstantiateViewController("BaseViewController") as BaseViewController;
                            var baseNav = new UINavigationController(ViewController);
                            NavigationController.PopToViewController(baseNav, true);
    
    
        }
    
  • LandLuLandLu Member, Xamarin Team Xamurai

    I noticed you created a new NavigationController and it wasn't located on your current window. So PopToViewController won't work.
    BaseViewController needs to be pushed into navigation stack first before poping. Try to pop to certain view controller like:

    var controllers = NavigationController.ViewControllers;
    BaseViewController baseViewController = null;
    foreach(var viewController in controllers)
    {
        if (viewController is BaseViewController)
        {
            baseViewController = viewController as BaseViewController;
            break;
        }
    }
    if (baseViewController != null)
    {
        NavigationController.PopToViewController(baseViewController, true);
    }
    else
    {
        NavigationController.PopToRootViewController(true);
    }
    
Sign In or Register to comment.