Xamarin iOS NavigationController returns 空

Xamarin iOS NavigationController returns null

我仍在学习 Xamarin 中的诀窍 ios 并根据以下示例 Monotouch.SlideoutNavigation 实现了侧抽屉。在本教程中,有一个主视图控制器 class,然后分配一个主导航控制器和一个侧边菜单。

抽屉菜单选项被送入菜单 class,而 "home screen/first screen" 被传递到主导航控制器 class,它是 UINavigationController 的子class class.

我的主屏幕是一个 tabcontroller class,我试图在这个 class 中引用导航控制器,但它总是 returns 空。

这是我面临的两个挑战:

这是 AppDelegate class :

[Register ("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
        public SlideoutNavigationController Menu { get; private set; }

 public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
    {
                Menu = new SlideoutNavigationController ();

 var tabBarController = GetViewController (Main, "MainTabBarController");

                Menu.MainViewController = new MainNavigationController (tabBarController, Menu);
                Menu.MenuViewController = new MenuNavigationController (new MenuControllerLeft (), Menu) { NavigationBarHidden = true };
                SetRootViewController (Menu, false);

        return true;
    }
}

MainTabControllerclass

 public partial class MainTabBarController : UITabBarController
{
        UINavigationItem titleRequest,titleHome,titleSell;

  public MainTabBarController (IntPtr handle) : base (handle)
    {
  //Create an instance of our AppDelegate
         appDelegate = UIApplication.SharedApplication.Delegate as AppDelegate;

        //Get an instance of our Main.Storyboard
        var mainStoryboard = appDelegate.Main;

        var tab1 = appDelegate.GetViewController (mainStoryboard, "Tab1");

        var tab2 = appDelegate.GetViewController (mainStoryboard, "Tab2");

        var tab3 = appDelegate.GetViewController (mainStoryboard, "Tab3");


        var tabs = new UIViewController[] {
            tab1, tab2, tab3
        };

        this.SelectedIndex = 1;
        ViewControllers = tabs;
  }

  public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();


            if(this.SelectedIndex == 0)
            {

                titleRequest = new UINavigationItem ("TAB 1");
                this.NavigationController.NavigationBar.PushNavigationItem (titleRequest, true); // NavigationController here is null

            }else if(this.SelectedIndex == 1)
            {
                titleHome = new UINavigationItem ("TAB 2");
                this.NavigationController.NavigationBar.PushNavigationItem (titleHome, true);


            }else{

                titleSell = new UINavigationItem ("TAB 3");
                this.NavigationController.NavigationBar.PushNavigationItem (titleSell, true);
            }

  }
 }

MainNavigation 控制器class

 public class MainNavigationController : UINavigationController
{

 public MainNavigationController(UIViewController rootViewController, SlideoutNavigationController slideoutNavigationController)
        : this(rootViewController, slideoutNavigationController, 

            new UIBarButtonItem(UIImage.FromBundle("icon_sidemenu.png"), UIBarButtonItemStyle.Plain, (s, e) => {}))
    {
    }
  public MainNavigationController(UIViewController rootViewController, SlideoutNavigationController slideoutNavigationController, UIBarButtonItem openMenuButton)
        : base(rootViewController)
    {
        openMenuButton.Clicked += (s, e) => slideoutNavigationController.Open(true);
        rootViewController.NavigationItem.LeftBarButtonItem = openMenuButton;
    }

   public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        this.Delegate = new NavigationControllerDelegate();
        InteractivePopGestureRecognizer.Enabled = true;

    }
   public override void PushViewController(UIViewController viewController, bool animated)
    {
        // To avoid corruption of the navigation stack during animations disabled the pop gesture
        if (InteractivePopGestureRecognizer != null)
            InteractivePopGestureRecognizer.Enabled = false;
        base.PushViewController(viewController, animated);
    }

    private class NavigationControllerDelegate : UINavigationControllerDelegate
    {
        public override void DidShowViewController(UINavigationController navigationController, UIViewController viewController, bool animated)
        {
            // Enable the gesture after the view has been shown
            navigationController.InteractivePopGestureRecognizer.Enabled = true;
        }
    }
}

编辑 - 根据下面 Jason 的建议进行更改后的结果

谁能帮我看看我做错了什么。

在 AppDelegate 中执行此操作:

    tabs = new UITabBarController();
    tabs.ViewControllers = new UIViewController[]{ 
        new UINavigationController(new UIViewController() { Title = "Tab A" }),
        new UINavigationController(new UIViewController() { Title = "Tab B" }),
        new UINavigationController(new UIViewController() { Title = "Tab C" })
    };

    Menu = new SlideoutNavigationController();
    Menu.MainViewController = new MainNavigationController(tabs, Menu);
    Menu.MenuViewController = new MenuNavigationController(new DummyControllerLeft(), Menu) { NavigationBarHidden = true };

我终于找到了解决这个问题的方法。对于使用 Dillan 的解决方案并将 TabBarController class 作为菜单 class 之一的任何人,以下是我如何让它工作的。

  1. 除了 MainNavigationController class,我将 TabBarController class 包裹在 NavigationController 中。在 this.That 解决了 TabBarController 中对 NavigationController 的空引用后,我不必将每个选项卡包装在它自己的 NavigationController 中 class

  2. 为了解决每个选项卡中的标题被遮盖的问题,我找到了一个简单的解决方案:

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
    
        try{
    
                this.ViewControllerSelected += (object sender, UITabBarSelectionEventArgs e) => {
    
                switch(TabBar.SelectedItem.Title)
                {
                case"TAB 1" :
    
                    Title = "TAB 1";
                    break;
    
                case "TAB 2":   
                  Title = "TAB 2";
                    break;
    
                default:
                    Title = "TAB 3";
                    break;
                }
            };
    
    
        }catch(Exception e)
        {
            Console.WriteLine (e.Message);
        }
    }