Bar Tab Item Name Before Storyboard Appears
my app starts with a tab bar controller which has 5 tabs. At startup, the first one is listed with a name, but the other four have no name until I click on them. The name then appears depending on what language the user has. How do I set the name of the tabs before the tab bar appears? I am using storyboard. Is there a way to customize the title of the tab bar programmatically when everything else is done with a storyboard? I tried in AppDelegate something like [FirstViewController setTitle:NSLocalizedString(@"Titel1", nil)];
But I got an error that there is no class method for the setTitle selector. Thank.
source to share
I had the same problem today. I use storyboard too. In my case, I used the following method.
- I created a new Objective-C class named "MainTabBarViewController" as a subclass of "UITabBarController".
-
In my storyboard, in the "Identity inspector", I changed the "Custom Class" to "MainTabBarViewController".
-
In the "viewDidLoad" method in the "MainTabBarViewController" I added:
[[self.viewControllers objectAtIndex: 0] setTitle: NSLocalizedString (@ "home", nil)];
[[self.viewControllers objectAtIndex: 1] setTitle: NSLocalizedString (@ "statistics", zero)];
[[self.viewControllers objectAtIndex: 2] setTitle: NSLocalizedString (@ "settings", nil)];
[[self.viewControllers objectAtIndex: 3] setTitle: NSLocalizedString (@ "info", nil)];
I'm guessing it's not perferd, but it works great for me.
source to share
I had a configuration with two tabs like:
MainTabBarController : UITabBarController
+- MessagesNavigationController : UINavigationController
+- ContactsNavigationController : UINavigationController
In the configuration with Storyboard and ARC, I overridden the selector initWithCoder:
in the custom classes of my view controllers UITabBarController (in this case ContactsNavigationController
) and initialized tabBarItem.title
like this:
@implementation ContactsNavigationController
...
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self) {
self.tabBarItem.title = NSLocalizedString(@"Contacts", nil);
}
return self;
}
When using a storyboard, iOS calls (id)initWithCoder:
instead -(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
when the storyboard view controller is initialized during startup UITabBarController. Also note that viewDidLoad
it is not called until the view controller tab is selected from the tab bar.
source to share
In an application where you create view controllers, set the title property here (not in viewDidLoad
), for example:
vc1 = [[VC1 alloc] init];
vc1.title = @"List";
vc2 = [[VC2 alloc] init];
vc2.title = @"Map";
tabBarController.viewControllers = [[NSArray alloc] initWithObjects:vc1, vc2, nil];
source to share
If you look at the FirstViewController generated by Xcode using a method-based template Tab-Bar app - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
, you can see something like this:
if (self) {
//check your language
self.title = NSLocalizedString(@"First", @"First");
self.tabBarItem.image = [UIImage imageNamed:@"first"];
}
So, you need to check your language and then set the property title
, this will set the title on the tab bar and the navigation bar.
source to share
Try this, in every view you need to use this method
Suppose this is you first sight
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"homepage", @"homepage");
[self.tabBarItem setTag:0];
self.tabBarItem.image = [UIImage imageNamed:@"homepage"];
}
return self;
}
In your application delegate class write this code to add UITabBarController
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2,viewController3,viewController4,viewController5, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
Add the code above to
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {}
source to share