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
Related
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.
How to add navigation bar on to ViewBased application [templet]
The following code will present a new navigation controller.
MyViewController *v = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
UINavigationController *t = [[UINavigationController alloc] initWithRootViewController:v];
[self presentModalViewController:t animated:true];
[t release];
[v release];
Otherwise, u can add the navigation bar to the nib file associated with the viewcontroller directly.
In your AppDelegate.m-file, add this under applicationDidFinishLaunching (or something like that).
UINavigationBar *navigationBar = [[UINavigationBar alloc] init];
[window addSubview:navigationBar];
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.
my app is based on tabbar controller
now in my default view i am showing a viewController and lets say it has Button A, when user press A it should load a my tableviewController but nothing is happening??
-(IBAction)promo:(id)sender
{
aRoot= [[tableViewController alloc] initWithNibName:#"tableViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:aRoot animated:YES];
}
but its not loading anything no error even???
/////////// UPDATE
i did this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Promo *aPromo = [[Promo alloc] initWithNibName:nil bundle:nil];//button A is deifned on this VC
// then...
aNav = [[UINavigationController alloc] initWithRootViewController:aPromo];
// [pageOne release];
and in promoviewController
-(IBAction)promo:(id)sender
{atab= [[TableViewController alloc] initWithNibName:#"TableViewController" bundle:nil];
//TableViewController *atab1 = [[TableViewController alloc]initWithNibName:#"TableViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:atab animated:YES];
}
You need a navigationController to push a viewController.
you can't add a viewcontroller to a uitabbar if you want to use the navigation controller.
Where you make your tab bar controller you have to do this:
MyFirstTabViewController *pageOne = [[MyFirstTabeViewController alloc] initWithNibName:nil bundle:nil];
// then...
UINavigationController *ncOne = [[UINavigationController alloc] initWithRootViewController:pageOne];
[pageOne release];
then add ncOne to the tab bar instead of the view controller. :) Then your code in the question should work (given that you're properly declaring aRoot in the header).
EDIT
Start again... choose a view based application call it TabBarTest.
Right click on classes and add three new classes. They need to be subclasses of UIViewController or UITableViewController. Lets say they're called RootViewOne RootViewTwo and SecondaryViewController.
Then open TabBarTestViewController.m
Uncomment the viewDidLoad method.
You need to now put this code in that method:
UITabBarController *tbc = [[UITabBarController alloc] init];
NSMutableArray *viewControllers = [NSMutableArray array];
RootViewOne *vc1 = [[RootViewOne alloc] initWithNibName:nil bundle:nil];
UINavigationController *nc1 = [[UINavigationController alloc] initWithRootViewController:vc1];
nc1.view.backgroundColor = [UIColor redColor];
[viewControllers addObject:nc1];
[vc1 release];
RootViewTwo *vc2 = [[RootViewTwo alloc] initWithNibName:nil bundle:nil];
UINavigationController *nc2 = [[UINavigationController alloc] initWithRootViewController:vc2];
nc2.view.backgroundColor = [UIColor blueColor];
[viewControllers addObject:nc2];
[vc2 release];
[tbc setViewControllers:viewControllers animated:YES];
[self presentModalViewController:tbc animated:YES];
Now open RootViewOne.m and in viewDidLoad put this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Click to move through stack" forState:UIControlStateNormal];
[button addTarget:self action:#selector(moveToNextView:) forEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Now you're going to need a custom method:
-(void)moveToNextView:(id)selector {
SecondaryViewController *page = [[SecondaryViewController alloc] initWithNibName:nil bundle:nil];
page.title = #"Next Page";
page.view.backgroundColor = [UIColor greenColor];
[self.navigationController pushViewController:page animated:YES];
[page release];
}
This is only basic, but you should get an understanding of the kinad process you need to go through. I typed this straight into the browser so there may be spelling mistakes... watch out if you get any errors or warnings. Hopefully this can help you with your project.
Finally i solved this , i changed that VC to Navigation view Controller and then i can push the new view based on button tap,, thanks to thomas also who helped me a lot but i couldn't figure it out.
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.