2015-11-13 39 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