【发布时间】:2015-11-13 19:36:20
【问题描述】:
我仍在学习 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 建议的更改后的结果
【问题讨论】:
-
很难说清楚你在做什么,但是从每个选项卡开始都应该包含一个单独的导航控制器,每个导航控制器都将包含该选项卡的视图。 tab 控制器本身应该是根视图,它不应该包含在 Nav 控制器中。
-
感谢杰森的回复。我同意你的选项卡控制器是根视图,每个选项卡都有一个导航控制器。我已经尝试过了,并且很想坚持下去。挑战是我需要实现一个带有标签栏控制器的侧边导航抽屉,而且我在网络上找不到任何侧边抽屉不是根视图的实现。这让我别无选择,只能将其作为根视图而不是标签栏控制器。
-
什么是 MainNavigationController?为什么不直接将标签栏控制器分配给 Menu.MainViewController?
-
这是导航控制器类。所以我们基本上将 UIViewcontrollers 传递给它,以便将它们推送到堆栈上。我会在那里添加课程。
-
您不能将选项卡控制器包装在导航控制器中。每个选项卡都需要它自己的导航控制器。
标签: c# ios xamarin.ios