MonoTouch DialogViewController - 为什么它必须首先出现在UINavigationController中?

时间:2013-05-11 10:33:37

标签: xamarin.ios nested monotouch.dialog dialogviewcontroller

我想在UITabViewController中使用DialogViewController。

问题:嵌套元素没有显示导航栏,因此无法返回。

当我将我的类(继承自DialogViewController)推送到UINavigationController时,行为是正确的。如果我在UITabViewController的一个选项卡中使用相同的类(即使使用底层的UINavigationController),那么行为是错误的。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分: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;
        }
    }
}

现在运行该应用程序。 您可以看到,甚至嵌套元素也很好地显示在导航栏中。即使是多级嵌套。