2009-05-09 10 views
0

J'avais une application basée sur le contrôleur de navigation. Et j'ai décidé d'utiliser des barres d'onglets dans mon application.UITabBar view

Lorsque l'utilisateur appuie sur un certain élément de la barre d'onglets, je veux afficher un certain contrôleur de vue - et je veux programmer dans mon code lequel afficher. Je suis fatigué d'ajouter dans l'Interface Builder un contrôleur de navigation dans ma barre d'onglets, mais viewWillAppear de son contrôleur de vue n'est pas appelé.

Comment puis-je implémenter cette fonctionnalité?

Merci.

Répondre

1

Je ne sais pas si c'est la "bonne façon", mais voici comment je le fais habituellement avec trois onglets.

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

Dans la méthode init chaque viewController vous pouvez faire quelque chose comme:

[[self tabBarItem] setImage:[dataSource tabConImg]]; 
[[self tabBarItem] setTitle:[dataSource name]]; 
[[self navigationItem] setTitle:[dataSource navConName]]; 

Pour définir l'icône utilisée dans la barre d'onglet, le titre dans la barre d'onglets, et le titre vous élément de navigation .

Espérons que cela aide.