I have a Xamarin.Forms app that I'm building that ends up having a number of pages presented, some modal and some via navigation pages. On user action, I have to dismiss a whole bunch of them instantly. I couldn't find a way to do it in cross-platform code in Xamarin.Forms. Instead, I went into iOS specific code and wrote this:
UIViewController currentController = UIApplication.SharedApplication.KeyWindow.RootViewController;
while (currentController.PresentedViewController != null)
currentController = currentController.PresentedViewController;
if (currentController != null) {
UIViewController parent = currentController.PresentingViewController;
currentController.DismissViewController(false, null);
if (parent != null) {
UIViewController parentParent = parent.PresentingViewController;
parent.DismissViewController(false, null);
if (parentParent != null) {
UITabBarController tabBarController = (UITabBarController)parentParent.ChildViewControllers[0];
UIViewController takeAPaymentVC = tabBarController.ViewControllers[0];
UINavigationController nc = (UINavigationController)takeAPaymentVC;
nc.PopViewControllerAnimated(false);
}
}
}
Essentially, I used UIViewControllers to dismiss whatever controllers I needed to, without animations. Visually, this seems to work. I get the desired outcome in the app.
However, after the above runs, any modal pages I try to present using Xamarin.Forms Navigation.PushModalAsync() ends up giving me this error and nothing happens:
2014-09-01 23:33:09.727 takeapaymentiOS[1003:60b] Warning: Attempt to present on whose view is not in the window hierarchy!
Why am I getting this error? Am I not allowed to call UIViewController.DismissViewController() when using Xamarin.Forms?