自定义UITabBarController和UINavigationController
我正在开发一个适用于iOS5的应用程序,我不使用故事板或IB。 我正在创build一个自定义的UITabBarController
并在我的AppDelegate
我把它只有1个UINavigationController
(不能告诉为什么)4视图控制器。
它导致了一种行为,我只能从第一个选项卡推送新的VC,显然,这个选项卡打包到名为navController
的UINavigationController
:
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];
这是为什么? 我应该为每个标签创build一个单独的UINavigationController
吗? 我从苹果的文档中获取这些代码。
是的你可以。 在你的UITabBarController.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中:
self.window.rootViewController = self.yourUITabBarController;
我应该为每个选项卡创build一个单独的UINavigationController
如果你想在每个标签中导航,你应该添加embedded在navigationController中的每个viewController。
假设你有一个tabbarController。 现在你可以添加任何viewController或任何NavController在你的tabController。 NavController可以包含viewController。 但是你可能会困惑,你会使用navController或viewController。 你将使用viewController你不需要导航,我的意思是你不需要的地方。
这是一个代码示例,其中第一个视图只包含视图,第二个视图包含导航控制器。 您无法在第一个视图中推送新视图,但可以在第二个视图中轻松推送新视图。
-(void)addTabBarControllers { UIViewController *viewController1, *viewController2; viewController1 = [[[HomeView alloc] initWithNibName:@"HomeView" bundle:nil] autorelease]; viewController2 = [[[FloorPlanHome alloc] initWithNibName:@"FloorPlanHome" bundle:nil] autorelease]; UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:viewController2]; self.tabBarController = [[[UITabBarController alloc] init] autorelease]; self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, nav2, nil]; [[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:@"First View"]; [[self.tabBarController.tabBar.items objectAtIndex:1] setTitle:@"Second View"]; [[self.tabBarController.tabBar.items objectAtIndex:0] setImage:[UIImage imageNamed:@"first.png"]]; [[self.tabBarController.tabBar.items objectAtIndex:1] setImage:[UIImage imageNamed:@"second.png"]]; self.window.rootViewController = self.tabBarController; [self.window makeKeyAndVisible]; }
从AppDelegate中的didFinishLaunchingWithOptions调用此方法。 这里HomeView和FloorPlanView是两个不同的视图,你需要首先添加这些视图和类文件。
通过下面的链接引用我的答案(vidyanand)
UITabBarController问题
if(!self.tabBarController)self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate =自我;
NSMutableArray * localcontrollerarray = [[NSMutableArray alloc] initWithCapacity:2];
UIViewController * viewController1 = [[HomeViewController alloc] initWithNibName:@“HomeViewController”bundle:nil];
UINavigationController * navi1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
[localcontrollerarray addObject:navi1];
UIViewController * viewController2 = [[ScanViewController alloc] initWithNibName:@“ScanViewController”bundle:nil];
UINavigationController * navi2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
[localcontrollerarray addObject:navi2];
self.tabBarController.viewControllers = localcontrollerarray;
[self.window addSubview:self.tabBarController.view];