How to include tabbar and navigation controller in single app? - iphone

How to create a tabbar Controller and Navigation Bar Controller in a window based app? I am trying to include both controllers.

You can do this as follows...
Create project of navigationController type..
then in AppDelegate , create a tabBarController. Have an array of you Viewcontrollers as follows...
mTabBar = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:3];
TSDetailTaskController *mTSDetailTaskController = [[TSDetailTaskController alloc]initWithNibName:#"TSDetailTaskController" bundle:nil];
UINavigationController *mTaskNavBar=[[UINavigationController alloc]initWithRootViewController:mTSDetailTaskController];
mTaskNavBar.tabBarItem.title=#"Task List";
mTaskNavBar.tabBarItem.image =[UIImage imageNamed:#"glyphicons_114_list.png"];
[mTSDetailTaskController release];
mTSSearchController=[[TSSearchController alloc]initWithNibName:#"TSSearchController" bundle:nil];
UINavigationController *mSearchNavBar=[[UINavigationController alloc]initWithRootViewController:mTSSearchController];
mSearchNavBar.title=#"Search";
mSearchNavBar.tabBarItem.image=[UIImage imageNamed:#"glyphicons_009_search.png"];
[mTSSearchController release];
TSSettingController *mTSSettingController = [[TSSettingController alloc]initWithNibName:#"TSSettingController" bundle:nil];
UINavigationController *mSettingNavBar=[[UINavigationController alloc]initWithRootViewController:mTSSettingController];
mSettingNavBar.tabBarItem.title=#"Setting";
mSettingNavBar.tabBarItem.image=[UIImage imageNamed:#"glyphicons_280_settings.png"];
[mTSSettingController release];
[localViewControllersArray addObject:mTaskNavBar];
[localViewControllersArray addObject:mSearchNavBar];
[localViewControllersArray addObject:mSettingNavBar];
[mTaskNavBar release];
[mSearchNavBar release];
[mSettingNavBar release];
mTabBar.viewControllers = localViewControllersArray;
mTabBar.view.autoresizingMask==(UIViewAutoresizingFlexibleHeight);
[localViewControllersArray release];
[window addSubview:mTabBar.view];
[self.window makeKeyAndVisible];
return YES;
hope this will help you out..

Instead of viewcontrollers, add navigation contollers as each item for the tabbarcontroller.

You can also do it in the Interface Builder, just make sure the Tabbar controller is the root/master controller and inside it you can add as many navControllers as you need. Of course, the tabbar controller is the one added to the Window in the AppDelegate.m file.
I you dont want the tabbar to be visible from the begining, you can implement self.tabbarcontroller.tabbar.hidden = YES; in the viewDidLoad or viewWillAppear methods of each of the views you dont want the tabbar on.

Related

How to set title of Navigation Bar from a View Controller of a subview?

I've a view controller having navigation bar in starting.
After some UIViewConrollers I have added a tabbar controller where I've created four tab bars and four view controllers for each tab bar.Now if I create tabbars with navigation bars related to each tab it shows two navigation bars at top, if create tabbars without navigation bar it will show only one tabbar but I am not able add title, corresponding to each tab view controller, to that navigation bar
Here is the code in which I created the tab bar
EventDetailViewController *firstViewController = [[EventDetailViewController alloc]init];
firstViewController.tabBarItem = [[[UITabBarItem alloc]initWithTitle:#"Home" image:[UIImage imageNamed:#"Multimedia-Icon-Off.png"] tag:2]autorelease]; MoreInfo *secondViewController = [[MoreInfo alloc]init];
secondViewController.tabBarItem = [[[UITabBarItem alloc]initWithTitle:#"Info" image:[UIImage imageNamed:#"Review-Icon-Off.png"] tag:0]autorelease];
PlaceInfoViewController *thirdViewController = [[PlaceInfoViewController alloc]init];
thirdViewController.tabBarItem = [[[UITabBarItem alloc]initWithTitle:#"Lugar" image:[UIImage imageNamed:#"Place-icon-OFF.png"] tag:1]autorelease];
FavoritesViewController *forthViewController = [[FavoritesViewController alloc]init];
forthViewController.tabBarItem = [[[UITabBarItem alloc]initWithTitle:#"Compartir" image:[UIImage imageNamed:#"Compartir-Icon-Off.png"] tag:3]autorelease];
UITabBarController *tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
tabBarController.viewControllers = [[NSArray alloc] initWithObjects:firstViewController, secondViewController, thirdViewController, forthViewController, nil];
tabBarController.view.frame = self.view.frame;
[self.view addSubview:tabBarController.view];
[firstViewController release];
[secondViewController release];
[thirdViewController release];
[forthViewController release];
You can obtain a reference to a view controller's navigationItem and set its title.
[[aViewController navigationItem] setTitle:#"My Title"];
Note that you'll want to embed all your view controllers in navigation controllers if you've not already done so. That way they'll actually get navigation bars with navigation items and you won't have to draw any of that yourself.
NSMutableArray *tabs = [NSMutableArray array];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[tabs addObject:nav];
nav = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[tabs addObject:nav];
...
[tabBarController setViewControllers:tabs];
Edit: Your tab bar controller is inside a navigation controller, so you need to get a reference to the tab bar controller first. Try [[[self tabBarController] navigationItem] setTitle:...] in the viewDidLoad method of your view controllers.
Please go to the EventDetailViewController.m and define the below method.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.title = NSLocalizedString(#"Catalogues", #"Catalogues");
//self.tabBarItem.image = [UIImage imageNamed:#"book"];
}
return self;
}
Hope it will work then in each viewController.m define the above initializer.
You cannot set title for tabbar control.You have to use UINavigationBar on every viewcontrollers
For UINavigationBar:
Drag Navigation bar to your xib/nib then add the title or do it programmatically
I got the solution myself by passing the reference of the navigation Item of the parent to the child view controller..thanx everyone for helping... :)
you cannot set the title of navigation bar . set the title of view or sub views in navigation.
self.title=#"welcome"; simply set the title.

UITabBarController only showing half of its UITabBar (off screen)

My UITabBar is not completely showing after I present a UITabBarController from a UIViewController. Please can you tell me what I am doing wrong?
My code is:
//some method
LoggedInViewController *lvc = [[[LoggedInViewController alloc] initWithAccount:account] autorelease];
[self presentModalViewController:lvc animated:YES];
- (void)viewDidLoad
{
self.tabController = [[UITabBarController alloc] init];
LoggedInFeedNavigationController *navController = [[LoggedInFeedNavigationController alloc] initWithAccount:self.account];
[self.tabController setViewControllers:[NSArray arrayWithObject:navController]];
[self.view addSubview:self.tabController.view];
[super viewDidLoad];
}
It's not a good practice to do:
[viewController1.view addSubview:viewController2.view];
The point of the MVC design is lost. The view controller should get your data (from the model) and put it in the view. If you have more than one view just arrange the functionality of the views to accept the corresponding data.
So if you need a tab bar controller you should do the following:
// assuming you are in the same initial controller
UITabBarController* pTabBarControllerL = [[UITabBarController alloc] init];
MyFirstController* pFirstControllerL = [[MyFirstController alloc] init];
[pTabBarControllerL setViewControllers:[NSArray arrayWithObject:pFirstControllerL]];
// perhaps set more tab bar controller properties - button images and so on
[self presentModalViewController:pTabBarControllerL animated:YES];
// release the memory you do not need
-(void)viewDidLoad {
// do your work in pFirstControllerL
}
PS: You should not subclass UINavigationController and UITabBarController.
Actually according to the Apple's recommendations UITabBarViewController should be the root in the UIWindow hierarchy. We had hard times trying to put TabBar VCs or Navigation VCs not to the root.

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 to manage TabBarController on new view after viewController push action

I have a problem building applicatin with tabBarController.
There is no problem doing tabBarController with navigationController if I build it from AppDelegate.
But now I have experienced problem when I want to create new view with tabBarController (3 tabs and each has navigation controllers) after a push from previous navigation controller.
It simply doesnt work.
Here is the code:
MainViewController *mainViewController = [[MainViewController alloc] initWithNibName:#"MainView_iPhone" bundle:nil];
mainViewController.tabBarItem.title = #"First";
UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
DictionariesViewController *dictionariesViewController = [[DictionariesViewController alloc] initWithNibName:#"DictionariesView_iPhone" bundle:nil];
dictionariesViewController.tabBarItem.title = #"Second";
UINavigationController *dictionariesNavigationController = [[UINavigationController alloc] initWithRootViewController:dictionariesViewController];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:mainNavigationController, dictionariesNavigationController, nil];
[self.navigationController pushViewController:tabBarController animated:YES];
There is a problem after view is pushe to "First" controller. Application crashes...
Please for help.
Regards
Borut
What are you trying to do with the following code?
[self.navigationController pushViewController:tabBarController animated:YES];
You said that your app has 3 tabs and each of those tabs have a navigation controller. Therefore, what you should do is to add the navigation controllers to tabBarController.viewControllers (which you did), but then you need to set the tabBarController as the root view controller.
I have done it this way and it works:
registerViewController = [[RegisterViewController alloc] initWithNibName:#"RegisterView_iPhone" bundle:nil];
AppDelegate_Phone *delegatePhone = [[UIApplication sharedApplication] delegate];
[delegatePhone.firstViewController.navigationController pushViewController:registerViewController animated:YES];
Thanks for your help guys.

iphone-dev: TabBarItem help

I have am setting up my application like so (in applicationDidFinishLaunching):
mytable = [[[MyTableController alloc] initWithStyle:UITableViewStylePlain] retain];
UINavigationController *mynav = [[[UINavigationController alloc]initWithRootViewController:mytable] autorelease];
[mynav.view setFrame:CGRectMake(0,0,320,460)];
UIViewController *tab1 = [[tabBarController viewControllers] objectAtIndex:0];
[mytable setTitle:#"Chronological"];
mytable.navigationController = mynav;
[tab1.view addSubview:mynav.view];
[window addSubview:tab1.view];
where MyTableController extends UITableController and has a navigation controller property. tabBarController is an outlet via the main nib file. There are no other nib files.
I am now unable to add any buttons to the navigation controller. Everything I do is ignored. What am I doing wrong here?
Can you include the code where you set up the UITabBarController tabBarController? I'm guessing that you are not properly setting the viewControllers property. Use UITabBarController -setViewControllers:animated: with an array of view controllers to initialize the tab bar controller.
Try something like this:
mytable = [[MyTableController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *mynav = [[UINavigationController alloc] initWithRootViewController:mytable];
[tabBarController setViewControllers:[NSArray arrayWithObject:mynav] animated:NO];
[mynav release];
[mytable release];
[tabBarController viewWillAppear:NO];
[window addSubview:[tabBarController view]];
[tabBarController viewDidAppear:NO];