Vista UITabBar scelta programmaticamente alla spina
-
20-08-2019 - |
Domanda
Avevo un'applicazione basata su controller di navigazione. E ho deciso di utilizzare le barre delle schede nella mia applicazione.
Quando l'utente preme su un determinato elemento della barra delle schede, voglio visualizzare un determinato controller di visualizzazione e nel mio codice voglio scegliere a livello di codice quale visualizzare.
Ho provato ad aggiungere un Interface Controller a un controller di navigazione nella mia barra delle schede, ma viewWillAppear del suo controller di visualizzazione non viene chiamato.
Come posso implementare questa funzione?
Soluzione
Non so se sia il " modo giusto " ;, ma ecco come lo faccio di solito con tre schede.
- (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];
}
Nel metodo init ogni viewController puoi fare qualcosa del tipo:
[[self tabBarItem] setImage:[dataSource tabConImg]];
[[self tabBarItem] setTitle:[dataSource name]];
[[self navigationItem] setTitle:[dataSource navConName]];
Per impostare l'icona utilizzata nella barra delle schede, il titolo nella barra delle schede e il titolo dell'elemento di navigazione.