Xamarin iOS NavigationController返回null

时间:2015-11-13 19:36:20

标签: c# ios xamarin.ios

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

抽屉菜单选项被输入菜单类,而“主屏幕/第一屏幕”被传递到主导航控制器类,它是UINavigationController类的子类。

我的主屏幕是一个tabcontroller类,我试图在这个类中引用导航控制器,但它总是返回null。

这是我面临的两个挑战:

  • 选项卡控制器和单个选项卡视图控制器内的导航控制器始终为空
  • 我的各个标签控制器类的标题未显示在导航栏上。

这是 AppDelegate类

[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;
    }
}

MainTabController类

 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控制器类

 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在

下建议进行更改后的结果

Tabs displayed at the bottom the first time but no screen titles

Tabs disappear after clicking menu options 有人可以帮我看看我做错了什么。

2 个答案:

答案 0 :(得分:0)

在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 };

答案 1 :(得分:0)

我终于找到了解决这个问题的工作。对于任何使用Dillan解决方案且有TabBarController类作为Menu类之一的人来说,这就是我如何使用它。

  1. 除了MainNavigationController类之外,我将TabBarController类包装在NavigationController中。在此之后我没有将每个选项卡包装在它自己的NavigationController中。这解决了对TabBarController类内的NavigationController的空引用

  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);
        }
    }