虽然这个问题没有一些代码示例的帮助,但我做了一个小例子,希望解决您的问题。在这个例子中,我使用了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;
}
}
}
现在运行该应用程序。 您可以看到,即使嵌套元素在导航栏中也很好地显示。即使使用多层嵌套。
感谢您的示例代码,它工作正常。但我想从一个NavigationController作为根控制器开始。这样做我得到两个导航栏 - 这不是我想要的。还有什么建议? – Joerg 2013-05-12 20:25:54