I have 3 tabs and main navigation controller. I implements with code:
UIViewController *monitorController = [[[MonitorController alloc] initWithNibName:#"MonitorController" bundle:nil] autorelease];
UIViewController *dashboardController = [[[DashboardController alloc] initWithNibName:#"DashboardController" bundle:nil] autorelease];
UIViewController *settingsController = [[[SettingsController alloc] initWithNibName:#"SettingsController" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:monitorController, dashboardController, settingsController, nil];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:self.tabBarController] autorelease];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
I want to change navigation title when I select another tap, but no variant (see below) not working (there are no any title at navigation controller):
self.title = DASHBOARD_TITLE;
self.navigationController.title = DASHBOARD_TITLE;
How I can resolve this problem?
The UINavigationController will display the title of the UIViewController it's currently displaying, that is why you are not able to set it directly.
You may try to change the title on your UITabBarController, but I'm not sure the navigation controller will update it's title after it first appeared on screen.
But, Do you really need that Architecture?
Usually the UITabBarController is the root viewController and if you need UINavigationControllers they are place inside the Tab that need to have navigation capability.
It was design to be use that way.
Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.
Quoted from UITabBarController Class Reference
Sorry I misread your question, what you have to do is to implement -tabBar:didSelectItem: from UITabBarDelegate protocol, and then change the title according to the pressed tab.
Or in the ViewWillAppear method of your controllers set the title, the way you were doing it
Related
I used tabbed application template to start my new iPhone project.
I will have 4 tabs.
And I want to nave navigation bar at the top.
So I added this in AppDelegate:
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *navigationcontroller = [[UINavigationController alloc] initWithRootViewController:viewController1];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationcontroller, viewController2, nil];
And this added navigation bar only at the top of the first ViewController (first tab).
What I want is to add navigation bar for the whole application not just in one view.
This will enable me to jump from one view to another and back across tabs.
So what is the best way to programmatically make one navigation bar for all tab views?
UPDATE
Basically my main question is should I have one navigation bar for all views or each tab should have its own navigation bar?
What is best practice in "iDevelopers" world.
I'd rather have the self.window.rootViewController be a navigationController, and afterwards, just push your tabBarController or
NSMutableArray *viewControllers = [[NSMutableArray alloc] init];
// create someViewController
[viewControllers addObject:someView];
// create someViewController2
[viewControllers addObject:someView2];
UITabBarController *tabController = [[UITabBarController alloc] init];
[tabController setViewControllers:viewControllers];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:tabController]
self.window.rootViewController = navController;
If you want to have navigation bars on each controller, you should to have UINavigationController on each tab of your UITabBarController
Look an answer to this question.
You you need navigation bar you have to add UINavigationController as you did for each view controller in tab bar. So self.tabBarController.viewControllers will have all navigation controllers.
Its not good idea to switch across tabs without user intervention.
I've setup an App that has a Tab Bar controller as a screen in a navigation controller using the storyboard. I can navigate to it fine, but when I start drilling down through screens in one of the tabs, the tab bar disappears. If I navigate back to the first screen the tab bar is supposed to be on, the tab bar will reappear, but I would prefer if it was visible on the child screens. Is this possible or do the two view controllers just not play well together?
The tab bar controller always needs to be the root view controller. You can't put it INSIDE a navigation controller.
Even if it was possible, it wouldn't be good user interaction. What is it you're trying to do exactly (functional)?
I think what you want is to place the navigation controller as the first tab inside the tab bar controller instead. (not the other way around like you're describing)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MenuViewController * menuViewController=[[[MenuViewController alloc] initWithNibName:#"MenuViewController_iPhone" bundle:nil] autorelease];
menuViewController.hidesBottomBarWhenPushed=YES;
//menuViewController You can have your option here
UINavigationController * navigationController;
UINavigationController * navigationController2;
UINavigationController * navigationController3;
viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController_iPhone" bundle:nil] autorelease];
viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController_iPhone" bundle:nil] autorelease];
navigationController=[[UINavigationController alloc] initWithRootViewController:viewController1];
navigationController2=[[UINavigationController alloc] initWithRootViewController:viewController2];
navigationController3=[[UINavigationController alloc] initWithRootViewController:menuViewController];
UITabBarController * tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:menuViewController,navigationControllerFirst, viewController2, nil];
//self.tabBarController.tabBar.tintColor=[UIColor orangeColor];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return true;
}
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.. :)
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.
I have created a UItabbarcontroller and 2 views with UITableViews just using code (no IB stuff) and I want to now add a navigation bar at the top that will include add and edit buttons, however I seem to be tripping up and blowing my app up or adding navgation controller to a 3rd tab only.
Here is my main code for adding the tab bar and switching views
FYI - I am using XCode4
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.mainTabBar = [[UITabBarController alloc] init];
// create the 2 views
tableViewController* vc1 = [[tableViewController alloc] init];
tableViewController2* vc2 = [[tableViewController2 alloc] init];
// put them in an array
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
// for the tab bar
mainTabBar.viewControllers = controllers;
// Add the tab bar controller's current view as a subview of the window
[self.window addSubview:self.mainTabBar.view];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}
Where do you want navigation controller(s)? You have to create one for each tab you want one in the UITabBarController.
You add a navigationController in conjunction with the first view controller on its stack. Try this:
// create the controllers for UITabBarController
tableViewController *vc1 = [[[TableViewController alloc] init] autorelease];
navController *nav1 = [[[UINavigationController alloc] initWithRootViewController:vc1] autorelease];
tableViewController *vc2 = [[[TableViewController alloc] init] autorelease];
navController *nav2 = [[[UINavigationController alloc] initWithRootViewController:vc2] autorelease];
// put them in an array
NSArray *controllers = [NSArray arrayWithObjects:nav1, nav2, nil];
// rest of your code
Also note that you need to release anything you alloc or retain. You can do it as I did by adding autorelease when you initialize them or you can release them explicitly after you've added them to the controllers array.
You then configure the navigationItem for each view controller in its loadView or viewDidLoad method depending on how you implemented it.