问题
As I'm new in Xamarin.IOS, I'd like to ask a question. I've followed this example for adding UITabBarController in a Xamarin.IOS project.
When I initialized RootViewController by an instance of TabController, it works fine and I have all tabs. BUT my NavigationController set null ! it means that :
- NavigationItem will disappear
navigating between viewControllers are not possible by this code :
this.NavigationController.PushViewController(new ProfileViewController(), true);
because the NavigationController is null ! Here is my code in AppDelegate:
_tabController = new TabController();
_window.RootViewController = _tabController;
and my 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;
}
}
In additional, I have lots of UIViewControllers and I do all of them programmatically and I dont use StoryBoard !
回答1:
By wrapping your TabController
in a UINavigationController
.
_tabController = new TabController();
_window.RootViewController = new UINavigationController(_tabController);
This way NavigationController property won't be null and navigation can be done.
来源:https://stackoverflow.com/questions/47159814/adding-uitabbarcontroller-and-have-no-navigationcontroller