Combine UITabBarController with UINavigationController - iphone

I tried to use an "tabbed application" with a navigation bar in it. With the default the tab bar works fine, but I just can't gat a navigation bar. I found some stuff about pushing the navigation-bar and stuff like that, but all the stuff I found was some years ago, so don't gonna help me. And the recent stuff is outdated to, since iOS5 and the new version of Xcode..
Could anyone point me in the right direction to combine a to solve this problem?
Keep the following facts in mind please:
I'm developing for iOS5
I'm using Xcode 4.2

Here's how you can achieve it programmatically.
Delete the reference to your main xib in [appName]-Info.plist
In main.m, load your delegate:
int retVal = UIApplicationMain(argc, argv, nil, #"myAppDelegate");
In the app delegate, load the tabBar, the navigation controller and the view in the navigationController.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// create window since nib is not.
CGRect windowBounds = [[UIScreen mainScreen] applicationFrame];
windowBounds.origin.y = 0.0;
[self setWindow:[[UIWindow alloc] initWithFrame:windowBounds]];
// View Controllers for tabController (one viewController per tab)
NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
// first tab has view controller in navigation controller
FirstView *firstView = [[FirstView alloc] initWithNibName:#"FirstView" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstView];
[viewControllers addObject:navController];
SecondView *secondView = [[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
[viewControllers addObject:secondView];
// create the tab controller and add the view controllers
UITabBarController *tabController = [[UITabBarController alloc] init];
[tabController setViewControllers:viewControllers];
// add tabbar and show
[[self window] addSubview:[tabController view]];
[self.window makeKeyAndVisible];
return YES;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
NSMutableArray *arrayViewController = [[NSMutableArray alloc] init];
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController_iPhone" bundle:nil];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
[arrayViewController addObject:navigationController1];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController_iPhone" bundle:nil];
UINavigationController *navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
[arrayViewController addObject:navigationController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = arrayViewController;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}

In iOS 5 it is no longer tolerated to change the view controller for a tab (no problem before iOS5). The only accepted controller is that defined in IB for that tab. So it is neccessary to install a navigation controller on this tab and give his view the navigation bar. Then you can push or pop your desired views without changing the tab's controller.

The basic theory is that you create a UITabBarController, and then put a UINavigationController inside that, and then put a UIViewController as the root view controller of the navigation controller. bryanmac just answered with a good code sample.

Related

Implement TabBar Application

I make an app in which First 4 screen has no tab bar But after that each screen has tab Bar.
So i added the tabbar in each nib file.
how can i implement the tabbar so it is working.
Help me!!
Without seeing code it's difficult to see where the error has been made, so i recommend you give the dev centre a go :)
Create tabBarController in your didFinishLaunching, but show it only after you show your first 4 screens without TabBar. This is a default didFinishLaunching, that is generated by Xcode when you chose standart TabBar app template:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Here you should replace this line:
self.window.rootViewController = self.tabBarController;
with your line of showing your controller. Something like this:
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
loginViewController.delegate = self;
self.window.rootViewController = loginViewController;
Then, when you remove your last screen and want to show the tab bar, write this:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.window.rootViewController = self.tabBarController;
The main idea is that you just change the rootViewController of your app window.

Change tab bar item title and color programmaticaly

I have implemented a tab bar controller in my App Delegate, but it's just empty squares in the tab bar. I wish to could change title and images of them and also I want to know how use not only custom image I add, but "default" images implemented in Xcode ("calculator" image, "search" image).
If you have tab bar in a xib, you can see it in tab bar item -> attributes inspector -> Identifier, then there is a list, if you don't want to use custom images. So there is my appDelegate.m code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after app launch
UIViewController *banksList = [[FailedBanksListViewController alloc] initWithNibName:#"FailedBanksListViewController" bundle:nil];
UINavigationController *listNavigationController = [[UINavigationController alloc] initWithRootViewController:banksList];
UIViewController *first = [[BIDViewController alloc] initWithNibName:#"BIDViewController" bundle:nil];
UIViewController *second = [[BIDDailyCount alloc] initWithNibName:#"BIDDailyCount" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:first,second,listNavigationController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
You have to create your UITabBarItems your self.
In you appdelegate you could do something like:
UIViewController *banksList = [[FailedBanksListViewController alloc] initWithNibName:#"FailedBanksListViewController" bundle:nil];
banksList.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0];
return self;
It might be a good idea to move this to a custom initializer in the controllers for each tab.

In iPhone App ,How to add tab bar controller on some particular view controller Programmatically?

In iPhone App ,How to add tab bar controller on some particular view controller Programmatically?
Here the viewcontroller Class is is UITableviewcontroller .
right now if i am adding the tab bar it appears some where in table view. i want to display it on bottom of window and and tableview should scroll separately from Tabbarcontroller
Please help and suggest
Thanks
FirstView *view1 = [[FirstView alloc]init];
SecondView *view2 = [[SecondView alloc] init];
UINavigationController *firstview = [[UINavigationController alloc] initWithRootViewController:view1];
UINavigationController *secondview = [[UINavigationController alloc] initWithRootViewController:view2];
UITabBarController* tabBar = [[UITabBarController alloc] init];
[tabBar setViewControllers:[NSArray arrayWithObjects:view1, view2, nil]];
[tabBar setDelegate:self];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:tabBar];
[self.view addSubview:navigationController.view];
Try this. here FirstView, SecondView are the class. if you click the tab, corresponding class (view) will open. And you have declare the tabbar and navigation controller delegate.
first you just select tabbar base application from project template.
now double click on mainwindow.xib its open it in interface builder.
now click on tabbarcontroller and open inspector window.
in above image your are able to see list of viewcontrollers. click on class tab will gives you the option of controller type. see in below image
if your are selecting table view controller then your first controller is table view. and tabbar is not in your view. table is scroll separately.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
//************ WRITE THE CODE OVER HERE FOR THE CREATION OF THE TAB BAR BY CODING**************
tabbar_object=[[UITabBarController alloc] init];
self.tabbarViewController=[[[TabBarViewController alloc] initWithNibName:#"TabBarViewController" bundle:nil] autorelease];
self.viewcontroller1=[[[FirstViewControllerForTabBar alloc] initWithNibName:#"FirstViewControllerForTabBar" bundle:nil] autorelease];
self.viewcontroller2=[[[SecondViewControllerForTabBar alloc] initWithNibName:#"SecondViewControllerForTabBar" bundle:nil] autorelease];
UINavigationController *tabbatnavigation=[[UINavigationController alloc] initWithRootViewController:tabbarViewController];
tabbatnavigation.title=#"First Tab";
UINavigationController *navigation1=[[UINavigationController alloc] initWithRootViewController:viewcontroller1];
navigation1.title=#"Sign Up";
UINavigationController *navigation2=[[UINavigationController alloc] initWithRootViewController:viewcontroller2];
navigation2.title=#"Sign Out";
tabbar_object.viewControllers=[[NSArray alloc] initWithObjects:navigation1,navigation2,tabbatnavigation, nil];
[tabbar_object setSelectedIndex:0];
//************************************************************************************************
self.window.rootViewController = tabbar_object;
[self.window makeKeyAndVisible];
return YES;
}

How to Use TabBarViewController with a NavigationController

I know I can do this
[self.navigationController pushViewController:self.someUITabBarController animated:YES];
And that means putting UITabBarController on a navigationgController somehow
What about if I want someUITabBarController to be the first controller (the one located on the lowest level) of navigationController?
I simply cannot change the rootViewController of the NavigationController into someUITabBarController
Erm not sure this is what you want. Below this code will be put under the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions in you "appDelegate" class.
UITabBarController *tabController = [[UITabBarController alloc] init];
UIViewController *viewController1 = ...
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController1];
NSArray *controllers = [NSArray arrayWithObjects:navigationController, nil]; // can add more if you want
[tabController setViewControllers:controllers];
// this is for custom title and image in the tabBar item
navigationController.tabBarItem.title = #"abc";
[navigationController.tabBarItem setImage:[UIImage imageNamed:#"abc.png"]];
self.window.rootViewController = tabController; // or [self.window addSubview: tabController.view];
[self.window makeKeyAndVisible];
I am not sure if this works. But try this,
UINavigationController *navCont = [[UINavigationController alloc] init];
[navCont pushViewController:navCont animated:NO];

navigate from one page to another in xcode

How can I navigate from one page to another page in xcode? remember without using the interface builder... I want the answer programmatically?
Pls be more precise if you want to get what you want.
If you are using view controllers within navigationcontroller you can make use of its pushViewController: or presentModalViewController:. Or if you just want to show another view you can just add the next view to existing view as subview.
Although your question is not much clear but still I would like to give a try...
You can use
UIViewController *yourViewController = [[YourViewControllerClass alloc] initWithNibName:#"<name of xib>" bundle:nil];
[self presentModalViewController:yourViewController animated:YES];
[yourViewController release];
In case the new view is also to be created programmatically, you can do that in the viewDidLoad method of YourViewControllerClass and change the initialization to
UIViewController *yourViewController = [[YourViewControllerClass alloc] init];
In YourViewController when you wish to come back to previous view on some button action you can use
[self dismissModalViewControllerAnimated:YES];
Another way that you can do is
UIViewController *yourViewController = [[YourViewControllerClass alloc] init];
[self addSubview:[yourViewController view]];
and to remove the view you can use
[self.view removeFromSuperview];
Hope this works for you, if yes please communicate....:)
//Appdelegate.m
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
return YES;
}
//In viewcontroller1.M
- (IBAction)GoToNext:(id)sender
{
ViewController2 *vc2 = [[ViewController2 alloc] init];
[self.navigationController pushViewController:vc2 animated:YES];
}