2015-11-13 40 views
0

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

当“主屏幕/第一屏幕”被传递到作为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); 
      } 

    } 
} 

的主导航控制器类

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

编辑 - 结果使下面

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

Tabs disappear after clicking menu options 由Jason建议的更改可能有人帮我看看,我在做什么错后。

+0

这一点很难告诉你在做什么,但每个选项卡,开始应该包含一个单独的导航控制器,每一个都将包含该标签的视图。选项卡控制器本身应该是根视图,它不应包含在导航控制器中。 – Jason

+0

感谢杰森的回应。我同意你在选项卡控制器是根视图和每个选项卡有一个导航控制器。我已经尝试过,并会喜欢坚持。面临的挑战是我需要实现一个带有标签栏控制器的侧面导航抽屉,并且我无法在Web上找到任何侧面抽屉不是根视图的实现。这让我别无选择,只能将其作为根视图而不是标签栏控制器。 – naffie

+0

什么是MainNavigationController?你为什么不直接将你的tabbar控制器直接分配给Menu.MainViewController? – Jason

回答

0

我终于找到了解决此工作。对于任何使用Dillan解决方案并且具有TabBarController类作为Menu类之一的人,以下是我如何使用它的方法。

  1. 我将TabBarController类包装在NavigationController中,除了MainNavigationController类之外。我没有包裹在它自己的NavigationController每个选项卡this.That解决TabBarController类

  2. 内空参考NavigationController为了解决被遮挡每个选项卡中的标题后,我发现了一个简单的解决方案:

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

我替换了这些更改,并在首次启动应用程序时,标签显示在底部,但导航栏上没有屏幕标题。点击抽屉并滚动浏览某些菜单选项并返回主页后,导航栏上的标题仍然不存在,而标签在底部消失。我已添加图片编辑。 – naffie

+0

你需要选择不同的菜单实现。我认为你正在使用的那个只是不能使用标签 - 它假定正在使用单个导航控制器。 Xamarin组件商店中提供了几种不同的产品。 – Jason

+0

你有什么特别的建议可以与标签一起使用吗?我已经尝试了FlyoutNavigation组件,并且它不能很好地与制表符配合使用。 – naffie