我有一个基于导航控制器的应用程序。我决定在我的应用程序中使用标签栏。
当用户按下某个标签栏项目时,我想显示某个视图控制器 - 我想在我的代码中以编程方式选择要显示的那个。
我尝试在界面生成器中添加一个导航控制器到我的标签栏中,但是没有调用其视图控制器的viewWillAppear。
如何实现此功能?
答案 0 :(得分:1)
我不知道这是不是“正确的方法”,但这是我通常用三个标签来做到这一点。
- (void)initControls {
// Create the window.
[self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
// Create Tab Bar.
tabCon = [[UITabBarController alloc] init];
// Local array variable that holds the viewcontrollers.
// Capacity corresponds to the number of VC's
NSMutableArray *localVCArray = [[NSMutableArray alloc] initWithCapacity:3];
MyFirstViewController *oneViewController = [[MyFirstViewController alloc] init];
UINavigationController *oneNavCon = [[UINavigationController alloc] initWithRootViewController:oneViewController];
[localVCArray addObject:oneNavCon];
[oneViewController release];
[oneNavCon release];
MySecondViewController *twoViewController = [[MySecondViewController alloc] init];
UINavigationController *twoNavCon = [[UINavigationController alloc] initWithRootViewController:twoViewController];
[localVCArray addObject:twoNavCon];
[twoViewController release];
[twoNavCon release];
MyThirdViewController *threeViewController = [[MyThirdViewController alloc] init];
UINavigationController *threeNavCon = [[UINavigationController alloc] initWithRootViewController:threeViewController];
[localVCArray addObject:threeNavCon];
[threeViewController release];
[threeNavCon release];
// Set the tab bars array of view controllers to the localVCArray
[[self tabCon] setViewControllers:localVCArray animated:YES];
// Release the localVCArray, all of its contents are now retained by tabCon.
[localVCArray release];
// Add controls to window and show.
[window addSubview:[tabCon view]];
[window makeKeyAndVisible];
}
在init方法中,每个viewController都可以执行以下操作:
[[self tabBarItem] setImage:[dataSource tabConImg]];
[[self tabBarItem] setTitle:[dataSource name]];
[[self navigationItem] setTitle:[dataSource navConName]];
设置标签栏中使用的图标,标签栏中的标题以及导航项的标题。