Joerg
Joerg

Reputation: 11

MonoTouch DialogViewController - why must it be in the first place of a UINavigationController?

I want to use a DialogViewController inside of a UITabViewController.

Problem: Nested elements don't show a navigation bar, and so it is not possible to go back.

When I push my class (inherited from DialogViewController) to a UINavigationController, then the behavior is correct. If I use the same class in a tab of a UITabViewController (even with an underlying UINavigationController), then the behaviour is wrong.

Can anyone help me out?

Upvotes: 1

Views: 1134

Answers (1)

Remco Koedoot
Remco Koedoot

Reputation: 239

Although the question is not assisted with some code sample, I made a small example hoping to solve your question. For this example I used the Tabbed Application template which comes with Xamarin.iOS and named it TabbingTest.

The following code goes in the AppDelegate. Change the FinishedLaunching method to contain:

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;
}

Then add the following methods:

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++;
}

Create a class named TestDialogController and paste the following code inside.

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;
        }
    }
}

Now run the application. You can see that even the nested elements show up nicely in the navigation bar. Even with multilevel nesting.

Upvotes: 5

Related Questions