2012-04-05 15 views
1

Ich habe eine App mit einem benutzerdefinierten UITabBarController, der fünf View-Controller enthält. Innerhalb jeder dieser View-Controller kann auf andere View-Controller zugegriffen werden. Im Idealfall möchte ich, dass mein benutzerdefinierter UITabBarController in jedem ViewController angezeigt wird - unabhängig davon, ob der View Controller direkt aus der Tableiste stammt oder nicht.Einen UITabbarController in jeder Ansicht sichtbar machen

I think Dies kann mit einem Navigation Controller in jedem der ursprünglichen fünf View-Controller erreicht werden, gibt es jedoch eine Möglichkeit, den benutzerdefinierten UITabBarController zu jedem View-Controller hinzuzufügen? Ich habe versucht, dies zu tun durch die folgenden Möglichkeiten in meinen viewDidLoad Methoden:

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

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

aber ich bad_access in meinen app-Delegaten, wenn ich den Code ausführen.

Irgendwelche Gedanken?

+0

Wie präsentieren Sie diese "andere Ansicht-Controller" von den View-Controller in Ihrer Registerkarten-Controller? – tux91

+0

In der Tat, mit 'UINavigationController' als Root-Controller für jede Registerkarte erhalten Sie, was Sie wollen. Gibt es einen bestimmten Grund, warum Sie sie nicht verwenden möchten? – Mutix

+0

mit: [self presentModalViewController: myviewController]; – coder

Antwort

2

Wie Sie richtig festgestellt haben, erreichen Sie mit den 'UINavigationControllern' als root-Controller jeder Registerkarte, was Sie zu tun versuchen. Hier

ist ein Beispiel dafür, wie man leicht Setup Ihre Tabbar mit Navigation Controller:

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

    ... 
} 

this helps :)

+0

Wow, das war wirklich einfach ... Danke – coder

1

Ihr AppDelegate sollte einen TabBarController haben. Dieser TabBarController enthält ein Array von ViewControllern (tabBarController.viewControllers). Diese ViewControllers sollten UINavigation Controller sein.

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

}

Sie präsentieren Ihre NavigationControllers nicht modal. Sie werden oben auf Ihrem TabBarController angezeigt und der TabBarController ist nicht mehr sichtbar. Versuchen Sie auch nicht, einen TabBarController in einem NavigationController darzustellen.

Verwandte Themen