Question

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.

Lorsque l'utilisateur appuie sur un élément de la barre d'onglets, je souhaite afficher un certain contrôleur de vue - et je souhaite par programme de choisir dans mon code celui qui sera affiché.

J'ai essayé d'ajouter à Interface Builder un contrôleur de navigation dans la barre d'onglets, mais viewWillAppear de son contrôleur de vue n'est pas appelé.

Comment puis-je implémenter cette fonctionnalité?

Était-ce utile?

La solution

Je ne sais pas s'il s'agit de la & "bonne manière &"; 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'onglets, le titre dans la barre d'onglets et le titre de l'élément de navigation.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top