UINavigationController within a UITabBarController, setting the navigation controller title - iphone

In my app I have a UINavigationController within a UITabBarController. Everything is working fine, however I can't set the title for the navigation controller. I have tried several different methods and googled around but there seems to be no solution for this problem.
Any Help would be greatly appreciated.
Thankyou in advance.
Sam

I found the answer in the end, it was:
self.tabBarController.navigationItem.title = #"title";

self.navigationItem.title = #"Your Title"
Works for every case.

For me the following works fine:
Initiate the controllers in appDelegateDidFinishLaunching:Method:
UINavigationController *navContr1;
UINavigationController *navContr2;
UIViewController *viewController1, *viewController2;
viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController_iPhone" bundle:nil] autorelease];
viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController_iPhone" bundle:nil] autorelease];
navContr1 = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];
navContr2 = [[[UINavigationController alloc] initWithRootViewController:viewController2] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
//self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navContr1, navContr2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
with this done, in your different viewControllers initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil - Method you can change the title with the following line:
self.title = #"Your Title";
Good Luck.

Look at the UIViewController class in the docs. There is a property pointing to the UINavigationItem which then has a property pointing to the title. If the pointer to the navigation item returns nil, you must have not wired the controllers together correctly.

The self.title always sets the tile of the rootViewController object of your ViewController.
If you have your viewController & the rootViewController is UINavigationController, then self.title will set the title of UINavigationController.
Similarly, if you have initialized a UITabViewController with the UIViewController objects, then the self.title will applies on the title corresponding button at index, on UITabBar of UITabViewController.
Hence said that, self.title applies on the object holding the viewController.

Related

iphone - Tabbar application with NavigationController, without MainWindow.xib

Hihi all,
I am pretty new in iPhone dev. I have follow some tutorial and created a tabbar application. Below is the code in the appdelegate implementation:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController *viewController1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
UIViewController *viewController2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
Then set the title and the image for the tab in each of the controller implementation.
My problem is that, example, in my viewController1, I need to navigate to viewController3, when I use presentModalViewController method to push the viewController3 in, the tabbar at the bottom will be disappeared.
While I tried to use the app delegate to refer to my tabBarController, and use tabBarController.navigationController pushViewController method, my viewController3 is not being pushed, and seems nothing happens.
I have tried to follow a few tutorial, but it's all required to drag in the navigationcontroller into the MainWindow.xib, which, in the xcode 4, MainWindow.xib doesn't exist anymore. How can i create the navigationcontroller from code so that the app can navigate between different view without hiding the tabbar?
Any comment is very much appreciated! Thanks in advance!
:)
If you want to use a navigation controller, you need to create a navigation controller. Since you're not using a XIB, you'll have to create it manually.
Since you want the tab bar to remain visible when you present viewController3, you need to make the navigation controller a child of the tab bar controller.
UIViewController *viewController1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UIViewController *viewController2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:
navController1,
viewController2,
nil];
Then when you want to present viewController3, do this:
// in some method of viewController1
[self.navigationController pushViewController:viewController3 animated:YES];
I am not very Sure but have you tried this??? Actually i am going to use XCode 4 soon, i am still using 3.2.8 version:-
WebViewController *viewController = [[WebViewController alloc]initWithNibName:#"WebViewController" bundle:nil];
viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:viewController animated:YES];
[viewController release];
See in this also the Tab bar will be removed when you navigate to your 3rd screen, you have to provide the navigation bar to come back.
Hope it helps.. :)

UITabBarController Does not shows tabbarItems name

I am creating app based on UTabbarController. I have creates that tab bar programmatically. Everything is running fine except I can not see the tabBatItem title. I have initialized everything properly, but when application launches all I can see is the first tabbar title. but if I select 2nd tabbaritem or so on I can see their names. I don't know whats going wrong here. Here is my code. Please let me know if I made any mistake.
Thanks.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
HomeViewController *viewController1 = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
UINavigationController*navController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
navController1.title=#"Home";
[viewController1 release];
TrainerTableViewController *viewController2 = [[TrainerTableViewController alloc] initWithNibName:#"TrainerTableViewController" bundle:nil];
UINavigationController*navController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
navController1.title=#"Trainer";
[viewController2 release];
SettingsTableViewController *viewController8 = [[[SettingsTableViewController alloc] initWithNibName:#"SettingsTableViewController" bundle:nil] autorelease];
UINavigationController*navController8=[[[UINavigationController alloc]initWithRootViewController:viewController8]autorelease];
navController1.title=#"Settings";
AboutUsViewController *viewController9 = [[[AboutUsViewController alloc] initWithNibName:#"AboutUsViewController" bundle:nil] autorelease];
UINavigationController*navController9=[[[UINavigationController alloc]initWithRootViewController:viewController9]autorelease];
navController1.title=#"About Us";
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navController1, navController2,navController8, navController9, nil];
[navController1 release];
[navController2 release];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
You can do this by including the code below inside the .m file of the view controller for each tab bar item. The code also includes how to change the image on the tab bar.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
self.title = #"Apply Now";
self.tabBarItem.image = [UIImage imageNamed:#"tbApplyNow.png"];
}
return self;
}
The best way to solve this problem is to set title of viewController not of navigationController
viewController1.tabBarItem.title = #"CohesiveSelf";
Do this for all tabs.
You're only setting the title for navController1. For each nab controller you create you need to set the title for that one.
Try this:
CHANGE:
navController1.title=#"Trainer";
TO
navController2.title=#"Trainer";
CHANGE:
navController1.title=#"Settings";
TO
navController8.title=#"Settings";
CHANGE:
navController1.title=#"About Us";
TO
navController9.title=#"About Us";
Also not you are not releasing navController8 or 9 which will cause in a memory leak
You are actually setting the title for the NavigationControllers. Also, watch that your code sets the title of navController1 multiple times, rather than setting it for the others.
You can set the titles for your tabBar by setting up tabBarItems for each controller.
You have the option of subclassing, or just including this in the application:didFinishLaunchingWithOptions: method in your AppDelegate.
Here's an example:
UITabBarItem *tbi1 = [navController1 tabBarItem];
[tbi1 setTitle:#"Home"];
UIImage *i1 = [UIImage imageNamed:#"hometabicon.png"];
[tbi1 setImage:i1];
This will set the title of tab1 to 'Home' and will set the tabBar icon to a file named 'hometabicon.png'.
You can repeat the same pattern for each of the other tabs.

Change view class on a tabbarcontroller

I have one tabbarcontroller set as main controller and its views were configured using interface builder. Now, programmaticaly, I would like to change default class view. How can it be done? For example, one tab view is set from class1 and I would like to set it from class2. Thank you.
I am not sure if this is what you meant, but to assign different view controllers programmatically to a tabbarcontroller you can use this:
UIViewController *viewController1 = [[FirstTab alloc] initWithNibName:#"FirstTab" bundle:NSBundle.mainBundle];
UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:viewController1];
UIViewController *viewController2 = [[SecondTab alloc] initWithNibName:#"SecondTab" bundle:NSBundle.mainBundle];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:viewController2];
myTabBarController = [[UITabBarController alloc] init];
myTabBarController.viewControllers = [NSArray arrayWithObjects:firstNavController, secondNavController, nil];
I would add and remove a tab bar item rather than changing the view controller of a single item.
Probably not animated.

Right design pattern for tabbed navigation views?

I've been stuck trying to puzzle this out for a couple days now, and I'll admit I need help.
The root view controller of my application is a tab bar controller. I want to have each tab bar a different navigation controller. These navigation controllers have completely different behavior.
So how do I set this up in terms of classes? Per Apple's documentation, I'm not supposed to subclass UINavigationViewController. So where do I put the code that drives each of these navigation controllers? Does it all get thrown in App Delegate? That would create an impossible mess.
This app should run on iOS 4.0 or later. (Realistically, I can probably require iOS 4.2.)
This is taken from one of my applications. As you say, you are not supposed to subclass UINavigationController, instead you use them as they are and you add viewcontroller on the UINavigationController's. Then after setting the root viewcontroller in each UINavigationController, you add the UINavigationController to the UITabBarController (phew!).
So each tab will "point" to a UINavigationController which has a regular viewcontroller as root viewcontroller, and it is the root viewcontroller (the one you add) that will be shown when a tab is pressed with a (optional) navigationbar at top.
UITabBarController *tvc = [[UITabBarController alloc] init];
self.tabBarController = tvc;
[tvc release];
// Instantiates three view-controllers which will be attached to the tabbar.
// Each view-controller is attached as rootviewcontroller in a navigationcontroller.
MainScreenViewController *vc1 = [[MainScreenViewController alloc] init];
PracticalMainViewController *vc2 = [[PracticalMainViewController alloc] init];
ExerciseViewController *vc3 = [[ExerciseViewController alloc] init];
UINavigationController *nvc1 = [[UINavigationController alloc] initWithRootViewController:vc1];
UINavigationController *nvc2 = [[UINavigationController alloc] initWithRootViewController:vc2];
UINavigationController *nvc3 = [[UINavigationController alloc] initWithRootViewController:vc3];
[vc1 release];
[vc2 release];
[vc3 release];
nvc1.navigationBar.barStyle = UIBarStyleBlack;
nvc2.navigationBar.barStyle = UIBarStyleBlack;
nvc3.navigationBar.barStyle = UIBarStyleBlack;
NSArray *controllers = [[NSArray alloc] initWithObjects:nvc1, nvc2, nvc3, nil];
[nvc1 release];
[nvc2 release];
[nvc3 release];
self.tabBarController.viewControllers = controllers;
[controllers release];
This is how I go from one viewcontroller to another one (this is done by tapping a cell in a tableview but as you see the pushViewController method can be used wherever you want).
(this is taken from another part of the app)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.detailedAnswerViewController == nil) {
TestAnsweredViewController *vc = [[TestAnsweredViewController alloc] init];
self.detailedAnswerViewController = vc;
[vc release];
}
[self.navigationController pushViewController:self.detailedAnswerViewController animated:YES];
}
The self.navigationcontroller property is of course set on each viewcontroller which are pushed on the UINavigationController hierachy.

How can i call the same ViewController from different TabBar items?

I've developed a ViewController that shows different data according to the input parameter. I would like to use a tabBar interface and call the same ViewController from different tabs by passing them to different parameters.
Can I do this? I get errors if I specify the ViewController's NIB in tabBar item.
Can you help me please?
Thanks in advance.
Create two different instances of your ViewController:
MyViewController *vc1 = [[MyViewController alloc] initWithNib:#"MyViewController" bundle:nil];
MyViewController *vc2 = [[MyViewController alloc] initWithNib:#"MyViewController" bundle:nil];
UITabBarController *tabs = [[UITabBarController alloc] init];
[tabs setViewControllers:[NSArray arrayWithObjects:vc1, vc2, nil] animated:NO];