我仍在學習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