我仍在学习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;
}
}
}
编辑 - 结果使下面
这一点很难告诉你在做什么,但每个选项卡,开始应该包含一个单独的导航控制器,每一个都将包含该标签的视图。选项卡控制器本身应该是根视图,它不应包含在导航控制器中。 – Jason
感谢杰森的回应。我同意你在选项卡控制器是根视图和每个选项卡有一个导航控制器。我已经尝试过,并会喜欢坚持。面临的挑战是我需要实现一个带有标签栏控制器的侧面导航抽屉,并且我无法在Web上找到任何侧面抽屉不是根视图的实现。这让我别无选择,只能将其作为根视图而不是标签栏控制器。 – naffie
什么是MainNavigationController?你为什么不直接将你的tabbar控制器直接分配给Menu.MainViewController? – Jason