Rinomina le schede in fase di esecuzione
-
29-09-2019 - |
Domanda
Ho un'app di barra delle schede che deve essere visualizzata in diverse lingue a seconda delle preferenze di un utente, ma non riesco a trovare molte informazioni su come modificare i nomi delle schede in fase di esecuzione. Ho bisogno delle schede per mostrare i nomi corretti all'avvio, non quando si accede alle schede.
Le migliori informazioni che ho trovato sono state in esecuzione
self.tabBarController.selectedIndex = 1;
tabBarController.selectedViewController.tabBarItem.title = @"Tab Name";
Dal delegato dell'app ma questo prima rende attiva la scheda e quindi imposta il nome.
Non esiste un modo migliore per impostare i nomi delle schede in fase di esecuzione? Idealmente vorrei metterli tutti in una volta sola.
Soluzione 2
Le 2 risposte non hanno funzionato per me, alla fine l'ho fatto in questo modo:
// Create temp strings to hold tab names
NSString *tab0Name;
NSString *tab1Name;
NSString *tab2Name;
NSString *tab3Name;
NSString *tab4Name;
// Set strings according to language
if ([UIAppDelegate.iStegAppLanguage isEqualToString:@"FR"])
{
tab0Name = @"Accueil";
tab1Name = @"Produits";
tab2Name = @"Caisse";
tab3Name = @"Branches";
tab4Name = @"Plus";
}
else if ([UIAppDelegate.iStegAppLanguage isEqualToString:@"IT"])
{
tab0Name = @"Home";
tab1Name = @"Prodotti";
tab2Name = @"Checkout";
tab3Name = @"Filiali";
tab4Name = @"More";
}
else if ([UIAppDelegate.iStegAppLanguage isEqualToString:@"EN"])
{
tab0Name = @"Home";
tab1Name = @"Products";
tab2Name = @"Checkout";
tab3Name = @"Branches";
tab4Name = @"More";
}
else // Default to german unless specifically set to another language
{
tab0Name = @"Home";
tab1Name = @"Produkte";
tab2Name = @"Checkout";
tab3Name = @"Filialen";
tab4Name = @"Mehr";
}
// Set tab name
self.tabBarController.selectedIndex = 1;
tabBarController.selectedViewController.tabBarItem.title = tab1Name;
self.tabBarController.selectedIndex = 2;
tabBarController.selectedViewController.tabBarItem.title = tab2Name;
self.tabBarController.selectedIndex = 3;
tabBarController.selectedViewController.tabBarItem.title = tab3Name;
self.tabBarController.selectedIndex = 4;
tabBarController.selectedViewController.tabBarItem.title = tab4Name;
self.tabBarController.selectedIndex = 0;
tabBarController.selectedViewController.tabBarItem.title = tab0Name; // Home last so it's shown first
Altri suggerimenti
Se hai un riferimento a Uitabbar, puoi usare qualcosa di simile:
for (UITabBarItem *tabBarItem in tabBar)
{
tabBarItem.title = NSLocalizedString(...);
}
Ti suggerisco di localizzare il tuo file XIB:
- Fare clic con il tasto destro sul tuo file xib
- "Ottenere informazioni"
- Scheda "Generale" in alto
- Pulsante "Rendi localizzabile" in basso
- Torna alla scheda "Generale" in alto
- Pulsante "Aggiungi localizzazione" in basso + Immettere la localizzazione che desideri (ad esempio "en", "fr", "lui", "ru" ecc.)
Ripeti l'ultimo passaggio fino a quando non hai tutte le lingue richieste.
Preferisco usare "en" invece dell'inglese predefinito che viene creato automaticamente - se preferisci anche "en", quindi eliminare il "inglese" alla fine ...
Ora puoi inserire diversi titoli nelle schede per ogni locale ...
Il modo in cui l'ho fatto è definendo i punti vendita nel delegato dell'app:
IBOutlet UITabBarItem *tabBarItem1;
IBOutlet UITabBarItem *tabBarItem2;
IBOutlet UITabBarItem *tabBarItem3;
IBOutlet UITabBarItem *tabBarItem4;
e poi, dopo aver collegato i punti vendita su IB, mettendolo a - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
:
[tabBarItem1 setTitle:NSLocalizedString(@"tab1", @"")];
[tabBarItem2 setTitle:NSLocalizedString(@"tab2", @"")];
[tabBarItem3 setTitle:NSLocalizedString(@"tab3", @"")];
[tabBarItem4 setTitle:NSLocalizedString(@"tab4", @"")];
Funziona ma non ne sono contento nemmeno - per qualche motivo non riesco a ottenere un mainwindow.xib localizzabile correttamente funzionante ..
Sto usando:
NSArray *itemsTabBar = [[NSArray alloc] initWithArray:[self.tabBarController.tabBar items]];
[[itemsTabBar objectAtIndex:0] setTitle:@"Contacts"];
[[itemsTabBar objectAtIndex:1] setTitle:@"Settings"];