2011-07-01 62 views
7

我正在嘗試在MonoTouch中同時使用UITabBarController和UINavigationController。Monotouch UITabBarController + UINavigationController

我可以創建一個基於導航的項目,並只使用navigationController進行導航,很好。或者我可以添加一個tabBarController並導航到幾個主屏幕,很好。

但是,我似乎無法導航到另一個不使用TabBarController如果存在。例如,我正在做一個處理「foo」的應用程序,所以我在我的tabbar,FooHome和FooSettings上有兩個視圖。如果用戶點擊FooSettings視圖中的「Add Foo」,我如何導航到新視圖。

NavigationController.PushToView似乎沒有任何效果,並且我不想將視圖添加到tabController,因爲它的漂亮且簡單,只有兩個項目。

我應該使用this.View.AddSubView嗎?想法類似於對話框的聲音,我只是不知道如何用monoTouch做...

回答

3

將您的FooHome和FooSettings控制器添加到UINavigationControllers並將這些導航控制器設置爲您的選項卡控制器。

因此,例如,第一個選項卡將包含一個導航控制器,其根控制器爲FooHome,第二個選項卡將包含一個導航控制器,其根控制器爲FooSettings。

當您點擊FooSettings中的添加Foo時,您會將新控制器推入第二個選項卡。

+0

謝謝。正是我所追求的。 – Kye

14

我在這裏摔了幾個小時,這篇文章有所幫助。非常感謝。對於那些仍然在黑暗中,這裏是我的代碼:

public override bool FinishedLaunching (UIApplication app, NSDictionary options) 
    { 
     // create a new window instance based on the screen size 
     window = new UIWindow (UIScreen.MainScreen.Bounds); 

     var vc1 = new VideosVC(); 
     var vc2 = new ScheduleVC(); 
     var vc3 = new TheCastVC(); 
     var vc4 = new MerchandiseVC(); 

     UINavigationController uinc1 = new UINavigationController(vc1); 
     UINavigationController uinc2 = new UINavigationController(vc2); 
     UINavigationController uinc3 = new UINavigationController(vc3); 
     UINavigationController uinc4 = new UINavigationController(vc4); 

     tabBarController = new UITabBarController(); 

     tabBarController.ViewControllers = new UIViewController [] { 
      uinc1, 
      uinc2, 
      uinc3, 
      uinc4, 
     }; 

     window.RootViewController = tabBarController; 

     window.MakeKeyAndVisible(); 

     return true; 
    } 
+2

+1爲您的代碼解決方案撰寫適合所有其他人的代碼! – scraimer

+0

確實,感謝您發佈您的代碼。我遇到了同樣的問題,上面的代碼對我來說實際上是即插即用的。 –

0

另一種方法是使用故事板來創建您的屏幕。通過UINavigationController使用UITabBarController非常簡單。你只需在它們之間創建賽格。花費你幾小時的時間可能在幾秒鐘內完成。另外,通過使用故事板,您可以快速重新排列視圖&選項卡而無需編寫代碼;使其更易於維護。

相關問題