如何检查当前的viewcontroller是被推送还是呈现

时间:2018-04-16 15:20:24

标签: c# xamarin.ios

我是Xamarin的新手。我已经搜索了很多,知道我的ViewController被推送或呈现。所以我会弹出它或相应地解雇它,但失败了。

2 个答案:

答案 0 :(得分:2)

您只需要检查导航堆栈中是否存在当前的viewcontroller。

解决方案

if(this.NavigationController == null)
{
    this.DismissViewController(true, null);
}
else
{
    this.NavigationController.PopViewController(true);
}

答案 1 :(得分:0)

您可以在方法

下使用
private static UIViewController GetController()

{
    var vc = UIApplication.SharedApplication.KeyWindow.RootViewController;
    while (vc.PresentedViewController != null  && vc.PresentedViewController.ToString().Contains("Xamarin_Forms_Platform_iOS_ModalWrapper"))
        vc = vc.PresentedViewController;
    return vc;
}

如果它的返回值不为null,则表示你在堆栈中有Present控制器

相关问题