how to add navigation controller and tab bar controller within viewcontroller? - iphone

I am developing one application which includes view controller because my first two pages content only view not tab-bar.after that i have created run time tab-bar controller using this code
UIViewController *viewcontroller1 = [[viewcontroller1 alloc] initWithNibName:#"viewcontroller1" bundle:nil];
viewcontroller1.title = #"sometext";
viewcontroller1.tabBarItem.image = [UIImage imageNamed:#"someimage.png"];
UIViewController *viewcontroller2 = [[viewcontroller2 alloc] initWithNibName:#"viewcontroller2" bundle:nil];
viewcontroller2.title = #"sometext";
viewcontroller2.tabBarItem.image = [UIImage imageNamed:#"someimage.png"];
tbc = [[UITabBarController alloc] initWithNibName:#"viewcontroller1" bundle:nil];
tbc.viewControllers = [NSArray arrayWithObjects: viewcontroller1,viewcontroller2, nil];
tbc.selectedViewController = viewcontroller1;
//// NSLog(#"Selected index = %d of %d", tbc.selectedIndex, [tbc.viewControllers count]);
[self presentModalViewController:tbc animated:NO];
it is working properly but in my second view i want tab-bar controller and navigation-controller both.
so in viewcontroller2 i have implemented code like that it's giving me navigation controller but it's hiding tab-bar controller
- (void)viewDidLoad
{
nvc = [[UINavigationController alloc] initWithRootViewController:[[viewcontroller2 alloc] initWithNibName:#"viewcontroller2" bundle:nil]];
[self presentModalViewController:nvc animated:NO];
[nvc release];
[super viewDidLoad];
}
so please help me what to i do so i can get both tab-bar controller & navigation-controller in this viewcontroller2.??
please guide me.

You can create new class for tabbar and in UI Design time you can add navigation controller as tab and set view in navigation controller. Whenever you want add tabbar controller in that page create object of tabbarcontroller class object and add it on view.

Related

View containing navigation bar and tab bar is shifted some 4mm downwards

in my app from a login screen i am navigating to a class say classA , like this
classA *objUserHome = [[classA alloc]init];
[self presentModalViewController:objUserHome animated:YES];
[objUserHome release];
and ClassA is having a navigating bar and a tabbar(5 tabs in it), i have created my tab bar programmatically like this
- (void)viewDidLoad
{
[super viewDidLoad];
//Create tab bar controller and navigation bar controller
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *arrControllers = [[NSMutableArray alloc] initWithCapacity:5];
//Add PunchClock to tab View Controller
PunchClock* objPunchClock = [[PunchClock alloc] initWithTabBar];
NavigationController = [[UINavigationController alloc] initWithRootViewController:objPunchClock];
NavigationController.navigationBar.tintColor = [UIColor brownColor];
[arrControllers addObject:NavigationController];
[NavigationController release];
[objPunchClock release];
//Add Time_Sheet to tab View Controller
Time_Sheet* objTime_Sheet = [[Time_Sheet alloc] initWithTabBar];
NavigationController = [[UINavigationController alloc] initWithRootViewController:objTime_Sheet];
NavigationController.navigationBar.tintColor = [UIColor brownColor];
[arrControllers addObject:NavigationController];
[NavigationController release];
[objTime_Sheet release];
//Add PTO to tab View Controller
PTO* objPTO = [[PTO alloc] initWithTabBar];
NavigationController = [[UINavigationController alloc] initWithRootViewController:objPTO];
NavigationController.navigationBar.tintColor = [UIColor brownColor];
[arrControllers addObject:NavigationController];
[NavigationController release];
[objPTO release];
//Add PunchClock to tab View Controller
CrewPunch* objCrewPunch = [[CrewPunch alloc] initWithTabBar];
NavigationController = [[UINavigationController alloc] initWithRootViewController:objCrewPunch];
NavigationController.navigationBar.tintColor = [UIColor brownColor];
[arrControllers addObject:NavigationController];
[NavigationController release];
[objCrewPunch release];
//Add PunchClock to tab View Controller
Reports* objReports = [[Reports alloc] initWithTabBar];
NavigationController = [[UINavigationController alloc] initWithRootViewController:objReports];
NavigationController.navigationBar.tintColor = [UIColor brownColor];
[arrControllers addObject:NavigationController];
[NavigationController release];
[objReports release];
// Add this view controller array into the tab bar
//self .viewControllers = arrControllers;
tabBarController .viewControllers = arrControllers;
[arrControllers release];
[self.view addSubview:[tabBarController view]];
}
And ClassA is inherited from UIViewController
now the problem is, after navigating to classA , view of classA is shifted some 4mm downwards why its so?? how can i fix this,,pls help me out ,, thanx in advance
When using storyboards and Modal Transition between 2 or more views you can encounter an error similar to the above.
If you use Modal Transition from ViewControllerA to ViewControllerZ and then attempt to Modal Transition from ViewControllerZ back to ViewControllerA sometimes the view of ViewControllerA gets pushed down the Window slightly.
This can be prevented using:
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
On ViewControllerZ to go back to ViewControllerA from an event on ViewControllerZ
You might have chosen some top bar in your Interface Builder or XIB file and additionally set the navigation bar. Dont choose any top bar in the XIB file.
Try as below
[self.navigationController.view addSubview:[tabBarController view]];
After a long research i finally fixed this issue just by inheriting the class from UINavigationController instead of UIViewControler

Keeping a UITabbarController visible in every view

I have an app with a custom UITabBarController that contains five view controllers. Within each of these view controllers, other view controllers can be accessed. Ideally, I would like my custom UITabBarController to appear in each ViewController-regardless on whether or not the view controller originates directly from the tabbar.
I think this can be accomplished using a Navigation Controller within each of the original five view controllers, however, is there a way to just add the custom UITabBarController to each view controller? I tried doing this by the following ways in my viewDidLoad methods:
AppDelegate *appDelegate = [(AppDelegate *)[UIApplication sharedApplication] delegate];
tabbarController = appDelegate.tabBarController;
tabbarController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:tabbarController.view];
but I get a bad_access in my app delegate when I run the code.
Any thoughts?
As you correctly stated, using 'UINavigationController's as root controllers of each tab will achieve what you are trying to do.
Here is an example of how to easily setup your tabbar with navigation controllers:
- (void)setupTabBar {
// Create nav-controller for local use
UINavigationController *localNavController;
// New tabbar controller and array to contain the view controllers
UITabBarController * theTabBarController = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
/*--------------------------------------------------------------------
* Setup the view controllers for the different tabs
*-------------------------------------------------------------------*/
// Root view controller for Tab 1
UIViewController *vc;
vc = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
localNavController = [[UINavigationController alloc] initWithRootViewController:vc];
localNavController.tabBarItem.image = [UIImage imageNamed:#"image.png"];
localNavController.tabBarItem.title = #"Tab1";
// Add navigation controller to the local vc array (1 of 4)
[localViewControllersArray addObject:localNavController];
// Root view controller for Tab 2
vc = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
localNavController = [[UINavigationController alloc] initWithRootViewController:vc];
localNavController.tabBarItem.image = [UIImage imageNamed:#"image.png"];
localNavController.tabBarItem.title = #"Tab2";
// Add navigation controller to the local vc array (2 of 4)
[localViewControllersArray addObject:localNavController];
// Root view controller for Tab 3
vc = [[ViewController3 alloc] initWithNibName:#"ViewController3" bundle:nil];
localNavController = [[UINavigationController alloc] initWithRootViewController:vc];
localNavController.tabBarItem.image = [UIImage imageNamed:#"image.png"];
localNavController.tabBarItem.title = #"Tab3";
// Add navigation controller to the local vc array (3 of 4)
[localViewControllersArray addObject:localNavController];
// Root view controller for Tab 4
vc = [[ViewController4 alloc] initWithNibName:#"ViewController4" bundle:nil];
localNavController = [[UINavigationController alloc] initWithRootViewController:vc];
localNavController.tabBarItem.image = [UIImage imageNamed:#"image.png"];
localNavController.tabBarItem.title = #"Tab4";
// Add navigation controller to the local vc array (4 of 4)
[localViewControllersArray addObject:localNavController];
// Point the tab bar controllers view controller array to the array
// of view controllers we just populated
theTabBarController.viewControllers = localViewControllersArray;
self.tabBarController = theTabBarController;
[self.window setRootViewController:self.tabBarController];
...
}
Hope this helps :)
Your AppDelegate should have one TabBarController. This TabBarController holds an array of ViewControllers (tabBarController.viewControllers).
These ViewControllers should be UINavigation Controllers.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController* navController1 = [[UINavigationController alloc] initWithRootViewController:firstOfYourControllers;
UINavigationController* navController2 = [[UINavigationController alloc] initWithRootViewController:sencondOfYourViewControllers;
UINavigationController* navController3 = [[UINavigationController alloc] initWithRootViewController:andSoOn;
UINavigationController* navController4 = [[UINavigationController alloc] initWithRootViewController:andSoOn;
UINavigationController* navController5 = [[UINavigationController alloc] initWithRootViewController:andSoOn;
NSArray* viewControllerArray = [NSArray arrayWithObjects:navController1, navController2, navController3, navController4, navController5, nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = viewControllerArray;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Do not present your NavigationControllers modally. They will be presented on top of your TabBarController and the TabBarController won't be visible anymore. Do also not try to present a TabBarController in a NavigationController.

UIInterfaceOrientation problem

My program is not supporting the UIInterfaceOrientation. Program will not support the UIInterfaceOrientation after I add the UITabBarItem.Please give a solution. Also I added the navigationController.
Here is my code.
-(void) applicationDidFinishLaunching:(UIApplication *)application {
//I create my navigation Controller
//UINavigationController *navigationController;
//I create my TabBar controlelr
tabBarController = [[UITabBarController alloc] init];
// Icreate the array that will contain all the View controlelr
NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:2];
// I create the view controller attached to the first item in the TabBar
sivajitvViewController *firstViewController;
firstViewController = [[sivajitvViewController alloc]init];
navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
//[navigationController.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
firstViewController.navigationItem.title=#"Gallery";
//viewController.tabBarItem.image = [UIImage imageNamed:#"natural.jpg"];
navigationController.tabBarItem.image = [UIImage imageNamed:#"Gallery.png"];
navigationController.tabBarItem.title = #"Gallery";
//navigationController.headerTitle = #"Some Title";
[localControllersArray addObject:navigationController];
[navigationController release];
[firstViewController release];
// I create the view controller attached to the second item in the TabBar
SecondViewController *secondViewController;
secondViewController = [[SecondViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
//[navigationController.tabBarItem initWithTabBarSystemItem:UITabBarSystemItemContacts tag:2];
navigationController.tabBarItem.image = [UIImage imageNamed:#"News.png"];
navigationController.tabBarItem.title = #"News";
[localControllersArray addObject:navigationController];
[navigationController release];
[secondViewController release];
// load up our tab bar controller with the view controllers
tabBarController.viewControllers = localControllersArray;
// release the array because the tab bar controller now has it
[localControllersArray release];
// add the tabBarController as a subview in the window
[window addSubview:tabBarController.view];
// need this last line to display the window (and tab bar controller)
[window makeKeyAndVisible];
}
If you have UITabBarController, all the tabs should support your interface orientation. So if you have 3 tabs and 2 of them support portrait and landscape, but the last one supports only portrait, you application will never turn to landscape.

How to push to new view controller from modal view that implements UITabBarController

A view is presented modally:
[self presentModalViewController:modalNavController animated:YES];
This view uses a UITabBarController with 4 elements. One of these elements, "Info" has a button that's only visible if its available. If the button is clicked, it needs to push to another view controller, but I'd also like to maintain the tab bar from it's parent view. I haven't been able to figure out how to do this with or without keeping the tab bar. Ive tried pushing and presentingModally in all the places that I could image. How should this be done properly?
Creating tab bar:
infoController.title = #"Info";
streetViewController.title = #"Street View";
reviewController.title = #"Reviews";
streetViewController.tabBarItem.image = [UIImage imageNamed:#"flag.png"];
infoController.tabBarItem.image = [UIImage imageNamed:#"openMarker.png"];
reviewController.tabBarItem.image = [UIImage imageNamed:#"reviews.png"];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = CGRectMake(0, 0, 320, 460);
UINavigationController *infoNC = [[[UINavigationController alloc] initWithRootViewController:infoController] autorelease];
infoNC.navigationBarHidden = YES;
[tabBarController setViewControllers:
[NSArray arrayWithObjects:infoNC, streetViewController, reviewController, nil]];
[self.view addSubview:tabBarController.view];
When you add the view controllers to the tab bar controller you need to do this:
MyCustomViewController *vc1 = [[MyCustomViewController alloc] initWithNibName:nil bundles:nil];
UINavigationController *nc1 = [[[UINavigationController alloc] initWithRootViewController:recipesRootView] autorelease];
[vc1 release];
then add nc1 instead of your custom view.
Then in MyCustomViewController to push another view controller do:
[self.navigationController pushViewController:(UIViewController *)page animated:YES];
That should work for you, and keep the tab bar controller.

problem in UITable cell selection

hai ,
i have coded in UITableview in the method as follows.but when i touch the cell or row ,it wont
go to the next page(navigation did not work).have i to declare navigation conroller in other file.but i have coded app delegate in applicationdidfinishmethod for tab bar through dynamic.how can i link navigation?
the code:
UITableview;(TableViewController)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SubController *nextController = [[SubController alloc] init];
[self.navigationController pushViewController:nextController animated:YES];
[nextController release];
}
appdelegation:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
tabBarController = [[UITabBarController alloc] init];
tabBarController.navigationItem.title = #" News";
TableViewController *rtbfViewController = [[TableViewController alloc]
init];
rtbfViewController.tabBarItem.title = #"News";
InfoViewController *infoViewController = [[InfoViewController alloc]
initWithStyle:UITableViewStyleGrouped];
infoViewController.tabBarItem.title = #"Info";
tabBarController.viewControllers = [NSArray
arrayWithObjects:rtbfViewController,infoViewController,nil];
tabBarController.customizableViewControllers = [NSArray arrayWithObjects:nil];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
The problem is, that you don't have a UINavigationController, so self.navigationController in your TableViewController is nil (and thus messages sent to this property are ignored). You should modify your code in the app delegate as follows:
// [...] create tab bar view controller...
// create navigation controller with TableViewController instance as root view controller
TableViewController *rtbfViewController = [[TableViewController alloc] init];
rtbfViewController.tabBarItem.title = #"News";
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rtbfViewController];
// [...] create other view controllers
// NOTE: add the navigation controller to the tab bar controller, rather than the TableViewController
tabBarController.viewControllers = [NSArray arrayWithObjects:navController,infoViewController,nil];
tabBarController.customizableViewControllers = [NSArray arrayWithObjects:nil];
And don't forget to release your view controllers afterwards:
[rtbfViewController release];
[navController release];
[infoViewController release];