添加UITabBarController并且没有NavigationController

时间:2017-11-07 13:59:29

标签: xamarin.ios uitabbarcontroller appdelegate navigationcontroller rootviewcontroller

因为我是Xamarin.IOS的新手,我想问一个问题。 我跟着this example在Xamarin.IOS项目中添加了UITabBarController。

当我通过TabController实例初始化RootViewController时,它工作正常,我有所有标签。 但我的NavigationController设置为null!它意味着:

  1. NavigationItem将消失
  2. 此代码无法在viewControllers之间导航:

    this.NavigationController.PushViewController(new ProfileViewController(), true);
    
  3. 因为NavigationController为null! 这是我在AppDelegate中的代码:

    _tabController = new TabController();
    _window.RootViewController = _tabController;
    

    和我的TabController:

    public class TabController : UITabBarController
        {
    
            UIViewController tab1, tab2, tab3, tab4;
    
            public TabController()
            {
                tab1 = new HomeViewController();
                tab1.TabBarItem.Image = UIImage.FromFile("Icons/Home.png");
    
                tab2 = new TagCategoryViewController(null, null, 1, null);
                tab2.TabBarItem.Image = UIImage.FromFile("Icons/Tag.png");
    
                tab3 = new SearchViewController();
                tab3.TabBarItem.Image = UIImage.FromFile("Icons/Search.png");
    
                tab4 = new ProfileViewController();
                tab4.TabBarItem.Image = UIImage.FromFile("Icons/Home.png");
    
                var tabs = new UIViewController[] {
                    tab1, tab2, tab3,tab4
                };
    
                ViewControllers = tabs;
            }
        }
    

    另外,我有很多UIViewControllers,我以编程方式完成所有这些操作,并且我不使用StoryBoard!

1 个答案:

答案 0 :(得分:2)

TabController包裹在UINavigationController

_tabController = new TabController();
_window.RootViewController = new UINavigationController(_tabController);

这样NavigationController属性不会为null,可以完成导航。