1

我有一个带有自定义 UITabBarController 的应用程序,其中包含五个视图控制器。在每个视图控制器中,可以访问其他视图控制器。理想情况下,我希望我的自定义 UITabBarController 出现在每个 ViewController 中,无论视图控制器是否直接源自标签栏。

认为这可以使用原始五个视图控制器中的每一个中的导航控制器来完成,但是,有没有办法将自定义 UITabBarController 添加到每个视图控制器?我尝试在我的方法中通过以下方式做到这一点viewDidLoad

AppDelegate *appDelegate = [(AppDelegate *)[UIApplication sharedApplication] delegate];
tabbarController = appDelegate.tabBarController;

tabbarController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:tabbarController.view];

但是当我运行代码时,我的应用程序委托中出现了 bad_access。

有什么想法吗?

4

2 回答 2

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];

    ...
}

希望这可以帮助 :)

于 2012-04-05T14:37:06.583 回答
1

您的 AppDelegate 应该有一个 TabBarController。tabBarController.viewControllers此 TabBarController 包含一个 ViewControllers ( )数组。这些 ViewController 应该是 UINavigation 控制器。

    - (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。

于 2012-04-05T14:36:56.893 回答