My app is a tab bar application, which one of the tabs is a TableViewController instead of a viewController which works fine (the table displays great) but where and how do I add UINavigationController to it? :-)
You can do this 2 different ways... in IB or code. If I'm dealing with a TabBar I usually do it in IB. All you have to do there is is drag a NavigationController object where your tableview object currently sits... then just make your tableviewcontroller the first child of your new navigation object.
TabBarController
-(Tab Bar)
-NavigationController
--(Navigation Item)
--TableViewController
or
If you want to do it in code... I would just set it up within your app delegate (usually because a tab bar is at the highest point in your app... meaning it appears right away after loading):
// Create a tabbar controller and an array to contain the view controllers
tabBarController = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:numberOfTabs];
// setup the view controllers
UINavigationController *myNavigationController;
myNavigationController = [[UINavigationController alloc] initWithRootViewController:myTableViewController];
// add to tab bar controller
[localViewControllersArray addObject:myNavigationController];
tabBarController.viewControllers = localViewControllersArray;
// add the tab bar to the window
[window addSubview:tabBarController.view];
You should then release the objects you just created since they will be retained by the TabBarController and Navigation Controller. Hope this helps
As Ryan noticed you can make it easily using IB. Here is how you can achieve this:
Launch Xcode and create new Tab Bar Application project.
Under resources group find MainWindow.xib and double click it to open in Interface Builder.
Next, select Tab Bar Controller object and open Inspector window (Command + Shift + I).
Notice "View Controllers" section in Inspector () then click on View Controller popup and change value from View Controller to Navigation Controller.
That's it! Now you can use your UITableViewController subclass inside this UINavigationController.
Related
My app works with tabbarcontroller as root view of the window, where on clicking each tab item loads splitviewcontroller with required views for it. The left and right panes of split views are navigation controllers. Now on any button action or didselectrow in tableview corresponding views are to be loaded in right pane. I succeeded loading views in right pane , but not able to display barbuttonitem when new view controller loaded in right pane of split view.
tabbarcontroller
-->splitviewcontroller
----->Leftpane:navigation controller
--------------->view controllers
----->Rightpane:navigation controller
--------------->view controller
Each Splitview of tab bar wil act like 'iPad Mail app' .
To make the app gernalised, I taken class RootiPadViewController which has the delegate of uisplitviewcontroller and uipopovercontroller which loads alls views in slpitview.
Loaded viewcontroller in right pane of split view as below.
UISplitViewController *splitViewController = (UISplitViewController*)[appDelegate.tabBarController.viewControllers objectAtIndex:tabIndex];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
//[navController pushViewController:viewController animated:YES];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[splitViewController.viewControllers objectAtIndex:0]];
[array addObject:navController];
splitViewController.viewControllers = array;
[array release];
Please suggest me why bar button item not displayed when views changed in splitviewcontroller.
App looks as below
I may be wrong but from my knowledge, the bar button item should appear only when you are in portrait mode, because:
a split view controller has two controllers (master and detail view controller)
both view controllers are shown on the screen when you are in landscape mode
when you are in portrait mode, only the detail view controller is shown, thus the bar button item appears
the bar button item goal is to let you open the master view controller in portrait mode
Please let me know if that helps you.
For Navigation Controller each view should define their left and right bar buttom items, if nothing defined then the tabbar will be empty . The only barbutton Item you will get free is the back barbutton item which appeared when you push a new View Controller above the rootViewController of the navigationController
You have to to allocate them in your ViewDidLoad method of each viewController in the NavigationControoler and set them as right and left barbutton item of your parentViewController(ie navigationController)
Please check this sample project https://github.com/alexth/TBSV
It's about how to use UISplitViewController inside UITabBar.
All logic is in Appdelegate's -loadSplitToTab Just total inheriting of all controllers, in every other cases UISplitViewController needs to be a root (as described in Apple documents) and you will not be able to use UISplitViewController inside UITabBar.
I have created a tabbarcontroller (with its default two view controllers) using interface builder in XCode 4.2.
But, when I run the application, the tab bar seems to be locked and I can't choose the other tab. Why is that?
PS: I haven't changed any property of the tab bar or tabbarcontroller in XCode.
How did you go about creating the tab bar? Did you have an initial view and then go to the Edit menu -> Embed In -> Tabbar Controller or did you start with nothing and drag in tab bar controller?
Either way, I just created a project with a single view and tried both ways - but the tab still worked. (if you do it by dragging the tab bar controller from the utility pane, you have to also select 'is initial view controller' if you replace the original view created with the project.
EDIT after your comments:
You don't really need to synthesize the tab bar controller in your AppDelegate - storyboarding will take care of this and you can reference it from code without needing generated synthesizers. Just design the layout in storyboard first by dragging in a tabbar controller (this will automatically create the two view controllers by default). Then select the tabbar controller and under the utilities panel, you'll see the 'is initial view controller' checkbox. Make sure it's checked. Then run your project.
Apologies - assumed because you were using XCode 4.2 that you were using storyboards. Perhaps you could try instantiating a tabbar controller fully in code instead of using IB at all. This is how I usually do it....
// Create the main tabbar controller
tabbarController = [[UITabBarController alloc] init];
// Create the first view controller
MyFirstViewController *v1 = [[MyFirstViewController alloc] initWithNibName:#"MyfirstViewController" bundle:nil];
[v1 setTitle:#"Tab1"];
// Create second view controller
MySecondViewController *v2 = [[MySecondViewController alloc] initWithNibName:#"MySecondViewController" bundle:nil];
[v2 setTitle:#"Tab2"];
// Make an array of controllers for the tabbar
NSArray *tabbarControllerArray = [NSArray arrayWithObjects:v1, v2, nil];
// Set the view controllers used by the tabbar controller
[tabbarController setViewControllers:tabbarControllerArray];
// Release views (retained elsewhere)
[v1 release];
[v2 release];
// Add the controller to the subview
[window addSubview:[tabbarController view]];
// Make key and visible
[self.window makeKeyAndVisible];
Give this way a shot and see how you get on.
I would like to add a TabBar to an existing view-based application I already started just to allow the user to switch to other parts of the app like the "About" section and another section entitled "Saved Searches" to display a navigational content (saved searches list > specific search result > product details).
Any idea on how to do this ? All tutorials I found point me directly to a TabBar template.
Thx for helping,
Stephane
You could start off with the UITabBar Application Template and you'll realize it's very easy to do:
In your UIApplicationDelegate class, in the method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Instantiate a UITabBarController like this:
UITabBarController *tabBar = [[UITabBarController alloc] init];
Then you set the view Controllers that will appear on the tab bar:
tabBar.viewControllers = viewControllers;
Which is a NSArray you can previously create with your UIViewController subclasses:
NSArray *viewControllers = [[[NSArray alloc] initWithObjects:vc1, vc2, vc3, nil] autorelease];
After this, you only have to set it as the root view controller of the window, or add it as a subview (it has the same effect, but the first approach doesnt work prior to iOS 4)
self.window.rootViewController = tabBar;
or
[self.window addSubView:tabBar.view];
And then
[tabBar release];
To achieve the kind of navigation that you say in your question, the view controllers you set to the tabBar should be instances of UINavigationController, which are very easy to create like this:
UINavigationController *vc1 = [[UINavigationController alloc] initWithRootViewController:firstViewControllerPage];
And inside them, you can push (navigate to) other view controllers doing:
[self.navigationController pushNavigationController:anotherViewController animated:YES];
Hope this brief review of it makes it a bit clear :)
You can create a new UITabBarController, and add it's view as a subview of your applications window. Then, add your other view controllers (for your "About" and "Saved Searches" sections) to that tab bar controller.
This can be done most easily in Interface Builder. In your MainWindow.xib, drag a Tab Bar Controller object onto the canvas. This will automatically create a tab bar with two items (one for each of the view controllers added). For each view controller under the tab bar controller, go to the identity inspector and change its class to your custom view controller subclass. Then, show the attributes inspector and there is a field "NIB Name" - again, set this to the appropriate nib name. Your custom controller views will then be loaded from their corresponding nib files. All that's left to do is name each tab in Interface Builder, and give it a graphic.
You can also do this programmatically if you don't like IB, by assigning the custom view controllers to the tab controller's viewControllers property, and assign a selectedViewController.
Hope this helps.
EDIT
Thought it might be helpful to show a little hierarchy! Your MainWindox.xib structure might look something like this:
AppDelegate
UIWindow
UITabBarController
UITabBar
AboutViewController (view loaded from "AboutViewController.xib")
Tab Bar Item - About
UINavigationController
Navigation Bar
SavedSearchesViewController - Root View Controller (view loaded from "SavedSearchesViewController.xib"
Tab Bar Item
And push appropriate view controllers from SavedSearchesViewController as normal to provide navigation content.
I have an application with a tab bar as the root controller. I have four items in the tabbar. The second item is a table view and this should have a navigation controller of course, the thing is that i don't even get the navigation bar to show up and i have no idea how to do it. So i tried to guess after a long time in google without success. This is what i did in the viewDidLoad method:
SearchNavController *navController = [[SearchNavController alloc]init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:navController];
[searchNavController release];
With that i got an IlegalArgumentException.
How can i get the navigation bar to show up?
Thanks in advance guys
Have a nice day!
Since you used interface builder to setup your tabbarcontroller. follow the following steps to get navigation bar and navigation controller in your tabbarcontroller:
Open Mainwindow.xib and select tababarcontroller .
In the attribute inspector , select the item1 in viewcontroller and from the arrows to the right change it to navigation controller.
Try this link:
link 1
Your navController should probably an ordinary UIViewController with the view you want to display. Also you need the following to actually display the navigationController:
[self presentModalViewController:navigationController animated:YES];
The navigation controller should now show up with your view as it's content.
IF you are using interface builder. Then Add UINavigation controller in each tab bar then add your view controller in uinavigation conrtroller. If you have 4 tabs then you have to put 4 navigation controller.
UITabbarcontroller ->>Tab Bar >>
UINavigationController >> set its class to your viewcontroller
HI all,
i am having navigation based application, in which i need too implement tab bars ,in one of view.
in one view i need 5 tabs, can any one please suggest me to create tab bars programmatically,, ? each tab should navigate to another xib.
Suggestions are always appreciated.
regards
Here is a sample code from Apple to create tab bar programmatically:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
tabBarController = [[UITabBarController alloc] init];
MyViewController* vc1 = [[MyViewController alloc] init];
MyOtherViewController* vc2 = [[MyOtherViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
tabBarController.viewControllers = controllers;
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
}
More here
It's worth noting that a tab bar controller implemented inside a navigation controller is unsupported by Apple. From the same doc linked to by vodhkang above:
Note: Although a navigation controller can be embedded inside a tab, the reverse is not true. Presenting a tab bar interface from within a navigation interface is potentially confusing for users. A navigation interface uses one or more custom view controllers to present an interface focused on one goal, which is usually the management of a specific type of data. By contrast, the tabs of a tab bar interface can reflect completely different purposes in an application and need not be related in any way. In addition, pushing a tab bar controller on a navigation stack would cause the tabs to be displayed for that screen only and not for any others.
So instead of using UITabBarController consider implementing a tab bar with a UIViewController as the tab bar's delegate.