【问题标题】:Pop the UIViewController in xamarin ios在 xamarin ios 中弹出 UIViewController
【发布时间】:2018-03-22 05:59:00
【问题描述】:

我想从内容页面打开一个 UIViewController,这段代码可以帮助我做到这一点

        UIWindow Window = new UIWindow(UIScreen.MainScreen.Bounds);
        var cvc = new ScanningPage();
        var navController = new UINavigationController(cvc);

        Window.RootViewController = navController;

        Window.MakeKeyAndVisible();

但我还想要那个控制器页面上的后退按钮,它可以将我导航回那个内容页面。

this.NavigationController.PopViewController(true);
or
this.DismissViewController(true,null);

在这种情况下不起作用。

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.ios


    【解决方案1】:

    我建议不要创建新的 Window 和 UINavigationController...

    Xamarin.Forms 包含在第一个窗口中的单个 VC 中,因此您可以通过以下方式获取该视图控制器:

    UIApplication.SharedApplication.Windows[0].RootViewController;
    

    因此,作为一个呈现和关闭 VC(来自表单或视图控制器)的依赖服务示例,您可以执行类似的操作。

    依赖接口:

    public interface IDynamicVC
    {
        void Show();
        void Dismiss();
    }
    

    iOS 依赖实现

    public class DynamicVC : IDynamicVC
    {
        UIViewController vc;
    
        public void Show()
        {
            if (vc != null) throw new Exception("DynamicVC already showing");
            vc = new UIViewController();
            var button = new UIButton(new CGRect(100, 100, 200, 200));
            button.SetTitle("Back", UIControlState.Normal);
            button.BackgroundColor = UIColor.Red;
            button.TouchUpInside += (object sender, EventArgs e) =>
            {
                Dismiss();
            };
            vc.Add(button);
            var rootVC = UIApplication.SharedApplication.Windows[0].RootViewController;
            rootVC.PresentViewController(vc, true, () => { });
        }
    
        public void Dismiss()
        {
            vc?.DismissViewController(true, () =>
            {
                vc.Dispose();
                vc = null;
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-15
      相关资源
      最近更新 更多