Vista de UITabBar elegida programáticamente de barril
-
20-08-2019 - |
Pregunta
Tenía una aplicación basada en el controlador de navegación. Y decidí usar barras de pestañas en mi aplicación.
Cuando el usuario presiona en un determinado elemento de la barra de pestañas, quiero mostrar un determinado controlador de vista, y quiero programáticamente en mi código elegir cuál mostrar.
Intenté agregar en el Creador de interfaces un controlador de navegación en mi barra de pestañas, pero no se llama a viewWillAppear de su controlador de vista.
¿Cómo puedo implementar esta función?
Solución
No sé si es la & "; en la forma correcta &"; pero así es como suelo hacer esto con tres pestañas.
- (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];
}
En el método init cada viewController puede hacer algo como:
[[self tabBarItem] setImage:[dataSource tabConImg]];
[[self tabBarItem] setTitle:[dataSource name]];
[[self navigationItem] setTitle:[dataSource navConName]];
Para establecer el icono utilizado en la barra de pestañas, el título en la barra de pestañas y el título de su elemento de navegación.