Custom UITabBarController and UINavigationController(自定义 UITabBarController 和 UINavigationController)
问题描述
我正在为 iOS5 及更高版本开发应用程序,我不使用情节提要或 IB.我正在创建一个自定义 UITabBarController
并在我的 AppDelegate
中放入 4 个视图控制器,其中只有 1 个 UINavigationController
(不知道为什么).
I'm developing an app for iOS5 and up and I don't use storyboards or IB. I'm creating a custom UITabBarController
and in my AppDelegate
I'm putting in it 4 view controllers with only 1 UINavigationController
(can't tell why).
这导致我只能从 first 选项卡推送新的 VC,这显然是打包到一个名为 navController
UINavigationController 中>:
It results in a behaviour where I can push new VC only from the first tab, which is apparently, packed into a UINavigationController
called navController
:
SGTabBarController *tabBarController = [[SGTabBarController alloc] init];
SGHomeViewController* vc1 = [[SGHomeViewController alloc] init];
SGChooseOSAgainViewController* vc3 = [[SGChooseOSAgainViewController alloc] init];
SGSmsServicesViewController* vc4 = [[SGSmsServicesViewController alloc] init];
SGSupportViewController *vc5 = [[SGSupportViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:vc1];
NSArray* controllers = [NSArray arrayWithObjects:navController, vc3, vc4, vc5, nil];
tabBarController.viewControllers = controllers;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = tabBarController;
[navController setNavigationBarHidden:YES animated:NO];
[self.window makeKeyAndVisible];
这是为什么呢?我应该为每个选项卡创建一个单独的 UINavigationController
吗?我从 Apple 的文档中获取了这段代码.
Why is that? Should I create a separate UINavigationController
for each tab? I took this code from Apple's documentation.
推荐答案
是的,你可以.尝试类似这样的代码在你的 UITabBarController.m 中:
Yes, you can. Try something like this code in yourUITabBarController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray* sectionViewControllers = nil;
NSArray* controllers = [self controllers];
UIViewController* controller = nil;
for (controller in controllers)
{
if (sectionViewControllers == nil)
sectionViewControllers = [NSMutableArray arrayWithCapacity:0];
UINavigationController* navigationController = [[UINavigationController allocWithZone:[self zone]] initWithRootViewController:controller];
navigationController.navigationBarHidden = YES;
[sectionViewControllers addObject:navigationController];
[navigationController release];
}
self.viewControllers = sectionViewControllers;
}
- (NSArray*)controllers
{
if (!_controllers)
_controllers = [NSArray arrayWithObjects:[self tabController1], [self tabController2], nil];
return _controllers;
}
这在你的 AppDelegate.m 中:
and this in you AppDelegate.m:
self.window.rootViewController = self.yourUITabBarController;
这篇关于自定义 UITabBarController 和 UINavigationController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:自定义 UITabBarController 和 UINavigationController


基础教程推荐
- :hover 状态不会在 iOS 上结束 2022-01-01
- 固定小数的Android Money Input 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01
- 如何使 UINavigationBar 背景透明? 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01
- LocationClient 与 LocationManager 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01