C# Xamarin iOS NavigationController返回null
我仍在学习Xamarin ios的诀窍,并基于以下示例实现了一个侧抽屉。在本教程中,有一个主视图控制器类,该类随后分配一个主导航控制器和一个侧菜单 抽屉菜单选项被输入菜单类,“主屏幕/第一个屏幕”被传递到主导航控制器类,主导航控制器类是UINavigationController类的子类 我的主屏幕是一个tabcontroller类,我试图引用该类中的导航控制器,但它总是返回null 以下是我面临的两大挑战:C# Xamarin iOS NavigationController返回null,c#,ios,xamarin.ios,C#,Ios,Xamarin.ios,我仍在学习Xamarin ios的诀窍,并基于以下示例实现了一个侧抽屉。在本教程中,有一个主视图控制器类,该类随后分配一个主导航控制器和一个侧菜单 抽屉菜单选项被输入菜单类,“主屏幕/第一个屏幕”被传递到主导航控制器类,主导航控制器类是UINavigationController类的子类 我的主屏幕是一个tabcontroller类,我试图引用该类中的导航控制器,但它总是返回null 以下是我面临的两大挑战: 选项卡控制器和单选项卡视图控制器中的导航控制器始终为空 导航栏上不显示我的各个选项卡
- 选项卡控制器和单选项卡视图控制器中的导航控制器始终为空
- 导航栏上不显示我的各个选项卡控制器类的标题
[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;
}
}
主选项卡控制器类
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;
}
}
}
主导航控制器类
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建议的更改后的结果
谁能帮我看看我做错了什么。在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类作为菜单类之一的人,下面是我如何让它工作的
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);
}
}
要知道您正在做什么有点困难,但首先,每个选项卡都应该包含一个单独的导航控制器,每个导航控制器都将包含该选项卡的视图。选项卡控制器本身应该是根视图,不应该包含在导航控制器中。感谢Jason的回复。我同意你的观点,选项卡控制器是根视图,每个选项卡都有一个导航控制器。我已经试过了,我很想坚持下去。挑战在于,我需要实现一个带有选项卡栏控制器的侧导航抽屉,在web上找不到任何侧抽屉不是根视图的实现。这让我别无选择,只能将其作为根视图而不是选项卡栏控制器。什么是MainNavigationController?为什么不直接将选项卡栏控制器分配给Menu.MainViewController?它是导航控制器类。我们基本上是把UIViewController传递给它,这样它就把它们推到堆栈上。我将在那里添加类。不能将选项卡控制器包装到导航控制器中。每个标签都需要自己的导航控制器。我将这些更改替换为第一次启动应用程序时,标签显示在底部,但导航栏上没有屏幕标题。单击抽屉并滚动某些菜单选项并返回主页后,导航栏上仍然没有标题,而底部的选项卡消失。我添加了图像编辑。您需要选择不同的菜单实现。我认为您正在使用的一个不适用于选项卡-它假定正在使用单个导航控制器。Xamarin组件商店中提供了几种不同的标签。您对标签有什么特别的建议吗?我已经试过了FlioutNavigation组件,但它不能很好地与选项卡配合使用。谢谢Jason的帮助。我决定把所有的标签都拿出来,然后只看菜单项。