我有一个带有自定义UITabBarController的应用程序,它包含五个视图控制器。在每个视图控制器中,可以访问其他视图控制器。理想情况下,我希望我的自定义UITabBarController出现在每个ViewController中 - 无论视图控制器是否直接来自tabbar。
我认为这可以使用原始五个视图控制器中的每个视图控制器中的导航控制器来完成,但是,有没有办法将自定义UITabBarController添加到每个视图控制器?我尝试通过viewDidLoad
方法中的以下方式执行此操作:
AppDelegate *appDelegate = [(AppDelegate *)[UIApplication sharedApplication] delegate];
tabbarController = appDelegate.tabBarController;
tabbarController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:tabbarController.view];
但是当我运行代码时,我的app委托中出现了bad_access。
有什么想法吗?
答案 0 :(得分:2)
正如您所说,使用'UINavigationController'作为每个标签的根控制器将实现您的目标。
以下是如何使用导航控制器轻松设置标签栏的示例:
- (void)setupTabBar {
// Create nav-controller for local use
UINavigationController *localNavController;
// New tabbar controller and array to contain the view controllers
UITabBarController * theTabBarController = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
/*--------------------------------------------------------------------
* Setup the view controllers for the different tabs
*-------------------------------------------------------------------*/
// Root view controller for Tab 1
UIViewController *vc;
vc = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil];
localNavController = [[UINavigationController alloc] initWithRootViewController:vc];
localNavController.tabBarItem.image = [UIImage imageNamed:@"image.png"];
localNavController.tabBarItem.title = @"Tab1";
// Add navigation controller to the local vc array (1 of 4)
[localViewControllersArray addObject:localNavController];
// Root view controller for Tab 2
vc = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
localNavController = [[UINavigationController alloc] initWithRootViewController:vc];
localNavController.tabBarItem.image = [UIImage imageNamed:@"image.png"];
localNavController.tabBarItem.title = @"Tab2";
// Add navigation controller to the local vc array (2 of 4)
[localViewControllersArray addObject:localNavController];
// Root view controller for Tab 3
vc = [[ViewController3 alloc] initWithNibName:@"ViewController3" bundle:nil];
localNavController = [[UINavigationController alloc] initWithRootViewController:vc];
localNavController.tabBarItem.image = [UIImage imageNamed:@"image.png"];
localNavController.tabBarItem.title = @"Tab3";
// Add navigation controller to the local vc array (3 of 4)
[localViewControllersArray addObject:localNavController];
// Root view controller for Tab 4
vc = [[ViewController4 alloc] initWithNibName:@"ViewController4" bundle:nil];
localNavController = [[UINavigationController alloc] initWithRootViewController:vc];
localNavController.tabBarItem.image = [UIImage imageNamed:@"image.png"];
localNavController.tabBarItem.title = @"Tab4";
// Add navigation controller to the local vc array (4 of 4)
[localViewControllersArray addObject:localNavController];
// Point the tab bar controllers view controller array to the array
// of view controllers we just populated
theTabBarController.viewControllers = localViewControllersArray;
self.tabBarController = theTabBarController;
[self.window setRootViewController:self.tabBarController];
...
}
希望这会有所帮助:)
答案 1 :(得分:1)
你的AppDelegate应该有一个TabBarController。这个TabBarController包含一个ViewControllers数组(tabBarController.viewControllers
)。
这些ViewControllers应该是UINavigation Controllers。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController* navController1 = [[UINavigationController alloc] initWithRootViewController:firstOfYourControllers;
UINavigationController* navController2 = [[UINavigationController alloc] initWithRootViewController:sencondOfYourViewControllers;
UINavigationController* navController3 = [[UINavigationController alloc] initWithRootViewController:andSoOn;
UINavigationController* navController4 = [[UINavigationController alloc] initWithRootViewController:andSoOn;
UINavigationController* navController5 = [[UINavigationController alloc] initWithRootViewController:andSoOn;
NSArray* viewControllerArray = [NSArray arrayWithObjects:navController1, navController2, navController3, navController4, navController5, nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = viewControllerArray;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
不要以模态方式呈现您的NavigationControllers。它们将显示在TabBarController之上,TabBarController将不再可见。也不要尝试在NavigationController中呈现TabBarController。