1

我想在UITabViewController中使用DialogViewController。MonoTouch DialogViewController - 爲什麼它必須在UINavigationController的第一位?

問題:嵌套元素不顯示導航欄,因此無法返回。

當我將我的類(從DialogViewController繼承)傳遞給UINavigationController時,則行爲是正確的。如果我在UITabViewController的選項卡中使用相同的類(即使使用底層的UINavigationController),那麼行爲也是錯誤的。

任何人都可以幫我嗎?

回答

5

雖然這個問題沒有一些代碼示例的幫助,但我做了一個小例子,希望解決您的問題。在這個例子中,我使用了Xamarin.iOS附帶的Tabbed Application模板,並將其命名爲TabbingTest。

下面的代碼放在AppDelegate中。更改FinishedLaunching方法包括:

public override bool FinishedLaunching (UIApplication app, NSDictionary options) 
{ 
    window = new UIWindow (UIScreen.MainScreen.Bounds); 

    var viewControllers = new UIViewController[] 
    { 
     CreateTabFor("Test", "first", new TestDialogController()), 
     CreateTabFor("Second", "second", new SecondViewController()), 
    }; 

    tabBarController = new UITabBarController(); 
    tabBarController.ViewControllers = viewControllers; 
    tabBarController.SelectedViewController = tabBarController.ViewControllers[0]; 

    window.RootViewController = tabBarController; 
    window.MakeKeyAndVisible(); 

    return true; 
} 

然後添加以下方法:

private int _createdSoFarCount = 0; 

private UIViewController CreateTabFor(string title, string imageName, UIViewController view) 
{ 
    var controller = new UINavigationController(); 
    controller.NavigationBar.TintColor = UIColor.Black; 
    var screen = view; 
    SetTitleAndTabBarItem(screen, title, imageName); 
    controller.PushViewController(screen, false); 
    return controller; 
} 

private void SetTitleAndTabBarItem(UIViewController screen, string title, string imageName) 
{ 
    screen.Title = NSBundle.MainBundle.LocalizedString (title, title); 
    screen.TabBarItem = new UITabBarItem(title, UIImage.FromBundle(imageName), 
             _createdSoFarCount); 
    _createdSoFarCount++; 
} 

創建一個名爲TestDialogController類和下面的代碼粘貼內。

using System; 
using MonoTouch.Dialog; 
using MonoTouch.UIKit; 

namespace TabbingTest 
{ 
    public class TestDialogController : DialogViewController 
    { 
     public TestDialogController(): base(UITableViewStyle.Plain,null,false) 
     {  
      var root = new RootElement ("Tabbing test"){ 
       new Section(){ 
        new RootElement ("First level", 0, 0) { 
         new Section (null, "This is the first level."){ 
          new RootElement ("Second level", 0, 0) { 
           new Section (null, "This is the second level."){ 
            new BooleanElement ("Flipflops", false) 
           } 
          } 
         } 
        }} 
      }; 

      this.Root = root; 
     } 
    } 
} 

現在運行該應用程序。 您可以看到,即使嵌套元素在導航欄中也很好地顯示。即使使用多層嵌套。

+0

感謝您的示例代碼,它工作正常。但我想從一個NavigationController作爲根控制器開始。這樣做我得到兩個導航欄 - 這不是我想要的。還有什麼建議? – Joerg 2013-05-12 20:25:54