Tab bar controller inside a navigation controller, how to push new viewcontrollers to the tabcontroller?

浪尽此生 提交于 2019-12-04 21:31:43

Here is the correct structure:

UITabBarcontroller (UIWindow's rootViewController)
->UINavigationController (first tab)
-->UIViewController
->UINavigationController (second tab)
-->UIViewController

It sounds like you want to change the layout of your view hierarchy to accommodate your requirements. You should present your view controllers as such:

UITabBarController -> UINavigationController -> UIViewController

In your app delegate, you can implement this programmatically using something along the lines of:

UIViewController *viewControllerOne = [[[UIViewController alloc] init] autorelease];
UINavigationController *navigationControllerOne = [[[UINavigationController alloc] initWithRootViewController:viewControllerOne] autorelease];

UIViewController *viewControllerTwo = [[[UIViewController alloc] init] autorelease];
UINavigationController *navigationControllerTwo = [[[UINavigationController alloc] initWithRootViewController:viewControllerTwo] autorelease];

UITabBarController *tabBarController = [[[UITabBarController alloc] init] autorelease];

[tabBarController setViewControllers:[NSArray arrayWithObjects:navigationControllerOne, navigationControllerTwo, nil]];

[[self window] setRootViewController:tabBarController]

I haven't checked the above, it's just written from memory but should do what you require as an example.

Using this format, you can push any additional view controllers on to the navigation controller stack without your tab bar disappearing.

If you want to push this view hierarchy without having the tab bar controller as your root view controller, simply push the tab bar controller instead of setting it as the root view controller in the app delegate.

Hope that helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!