I am working on a app, where I got a tabbar, in the 2nd tabitem, I got a UITableView, when the user presses the row on that UITableView it must navigate to another UITableView(2nd), once on the 2nd UITableView we can then navigate to detailview.
Can anyone help me out how to sort this out.
Help much appreciated.
- (void)viewDidLoad {
UITabBarController * t = [[UITabBarController alloc] init];
ResourcesViewController * c = [[ResourcesViewController alloc] init];
UINavigationController * n = [[UINavigationController alloc] initWithRootViewController:c];
[c release];
NSArray * tabs = [NSArray arrayWithObject:n];
[n release];
[t setViewControllers:tabs];
[window addSubview:[n view]];
//---initialize the array---
listOfResources = [[NSMutableArray alloc] init];
//---add items---
[listOfResources addObject:#"Speeches"];
[listOfResources addObject:#"Publications"];
[listOfResources addObject:#"Images/Photo Library"];
[super viewDidLoad];
}
This is ViewDidLoad method in the rootviewController and I created the next level UITableView called detailtableviewcontroller but can't call it from this method.
I think you might want the following view hierarcy (and corresponding controllers) to deal with this:
UITabBarController
UINavigationController
UITableViewController
UITableViewController
DetailViewController
AnotherViewControllerForTabBarButton
Your UITabBarController switches between views one indentation level below it, one of these views will be the UINavigationController which will provide the navigation between your various table views that you describe (1 and 2), as well as the detail view once you've drilled far enough down.
Hope that helps! :)
There are similar questions here:
How to : Navigation Controller in Tab Bar Controller
Tab Bar Application With Navigation Controller
Related
I am making a small app with the following view hierarchy with UINavigationController:
Login -> Options -> three different views
The problem is that I would like to navigate between the last 3 views in the following manner:
1<->2
1<->3
2<->3
i.e. to be able to switch to any view from any other view, which reminds UITabViewController functionality. So, it is not hierarchical, it is any-to-any graph. To switch between views I will use buttons in the navigation bar.
The easiest way for me is to subclass UINavigationController, add properties that correspond to my views and implement methods for switching between these views (using pushViewController and popToRootViewController). These methods will be called from the views for switching (navigating).
However the reference says that UINavigationController is not intended for subclassing.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
What do you recommend me to do?
I'll keep the UINavigationController but instead of using the usual pushViewController:, switch views like this:
NSMutableArray *viewControllers = [self.navigationController.viewControllers mutableCopy];
// from here you can modify the order of controllers as much as you want
[viewControllers addObject:nextViewController];
[viewControllers removeObject:self];
[self.navigationController setViewControllers:viewControllers animated:YES];
If you don't want how the animation turns out, you can set animated:NO and either enclose setViewControllers: in an [UIView animate...] block, or add your own custom CAAnimation to the navigation controller's layer.
Use the below code to add a view controller to a navigation controller,
Navigating From first -> second
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
NSMutableArray *navigationarray = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
[navigationarray removeAllObjects];
[navigationarray addObject:secondView];
self.navigationController.viewControllers = navigationarray;
Navigating From first -> third
ThirdViewController *thirdView = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
NSMutableArray *navigationarray = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
[navigationarray removeAllObjects];
[navigationarray addObject:thirdView];
self.navigationController.viewControllers = navigationarray;
The above code will removes all the viewControllers from the Navigation Array and places a fresh View Controller
If u want to go to a particular view controller, then use the below code...
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES]
Change the index to ur view controller in the stack.
My UITabBar is not completely showing after I present a UITabBarController from a UIViewController. Please can you tell me what I am doing wrong?
My code is:
//some method
LoggedInViewController *lvc = [[[LoggedInViewController alloc] initWithAccount:account] autorelease];
[self presentModalViewController:lvc animated:YES];
- (void)viewDidLoad
{
self.tabController = [[UITabBarController alloc] init];
LoggedInFeedNavigationController *navController = [[LoggedInFeedNavigationController alloc] initWithAccount:self.account];
[self.tabController setViewControllers:[NSArray arrayWithObject:navController]];
[self.view addSubview:self.tabController.view];
[super viewDidLoad];
}
It's not a good practice to do:
[viewController1.view addSubview:viewController2.view];
The point of the MVC design is lost. The view controller should get your data (from the model) and put it in the view. If you have more than one view just arrange the functionality of the views to accept the corresponding data.
So if you need a tab bar controller you should do the following:
// assuming you are in the same initial controller
UITabBarController* pTabBarControllerL = [[UITabBarController alloc] init];
MyFirstController* pFirstControllerL = [[MyFirstController alloc] init];
[pTabBarControllerL setViewControllers:[NSArray arrayWithObject:pFirstControllerL]];
// perhaps set more tab bar controller properties - button images and so on
[self presentModalViewController:pTabBarControllerL animated:YES];
// release the memory you do not need
-(void)viewDidLoad {
// do your work in pFirstControllerL
}
PS: You should not subclass UINavigationController and UITabBarController.
Actually according to the Apple's recommendations UITabBarViewController should be the root in the UIWindow hierarchy. We had hard times trying to put TabBar VCs or Navigation VCs not to the root.
I'm trying to use a UINavigationController but I'm uncertain how. Up till now (for about a year), I've been using presentModalViewController and dismissModalViewController to present/dismiss view controllers.
So, this is what I did. My main view controller (the first one that shows on launch) is called MainViewController, and it extends UIViewController.
So I made this launch function in my app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MainViewController *controller = [[MainViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
And in my MainViewController's viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Title";
self.navigationController.navigationBar.tintColor = [Constants barColor];
....more code...
}
But, in my MainViewController, I'd like to present another view controller called SecondViewController, which needs a UINavigationBar with a back arrow button. So do I make SecondViewController extend UIViewController and do the same thing by setting the title and backButton in the viewDidLoad method? And how do I present it? What should I do to accomplish this?
You'll need to set a root view controller up, it's easiest starting from the apple template.
Here's where the magic happens:
UIViewController *controller = [[UIViewController alloc] initWithNibName:#"MyNib" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
The nav controller does all the work for you (back buttons, titles, animations) - it keeps track!
My workflow is this:
Setup MutableArray in the viewDidLoad, add controllers to it, e.g:
NSMutableArray *array = [[NSMutableArray alloc] init];
MyCustomViewController *customView = [[MyCustomViewController alloc] initWithNibName:#"nib" bundle:#"nil"];
customView.title = #"Second Level";
[array addObject:customView];
self.controllers = array;
Then in your delegate:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
UIViewController *childControllerToBe = [controllers objectAtIndex:row];
[self.navigationController pushViewController:childControllerToBe animated:YES];
}
This, along with a lot more can be learnt by reading a decent beginner book such as Beginning iPhone Development
Also, apple docs are always good :)
UINavigationController is a subclass of UIViewController, but unlike UIViewController it’s not usually meant for you to subclass. This is because navigation controller itself is rarely customized beyond the visuals of the nav bar. An instance of UINavigationController can be created either in code or in an XIB file with relative ease.
Please visit "How to add UINavigationController Programmatically"
You should Push it onto the navigation stack.
This Lecture by Stanford's iPhone Course will teach you a lot about Navigation Bars. (It's a quick read)
Basically at the heart of it you need this code:
[self.navigationController pushViewController:SecondView];
You can use PopViewController to go back programmatically, but the Back Button is automatically created.
Here's some source code from the Lecture. It covers exactly what you are having issues with.
I am kind of stuck and would appreciate any ideas on what I did wrong.
I created programmatically a NSObject which holds a UITabBarController to which three ViewControllers are added:
UITabBarController tabBarController = [[UITabBarController alloc] init];
ControllerOne = [[OneViewController alloc] initWithNibName:#"OneView" bundle:nil];
ControllerTwo = [[TwoViewController alloc]initWithNibName:#"TwoView" bundle:nil];
ControllerThree = [[ThreeViewController alloc] initWithNibName:#"ThreeView" bundle:nil];
NSMutableArray *viewControllers = [[NSMutableArray alloc] initWithCapacity:3];
[viewControllers addObject:ControllerOne];
[viewControllers addObject:ControllerTwo];
[viewControllers addObject:ControllerThree];
[tabBarController setViewControllers:viewControllers];'
I now display the tabBarController's view
viewController.modalTransitionStyle = transitionStyle;
[self presentModalViewController:viewController animated:YES];
with viewController being the just created tabBarController.
The view changes fine, displaying the tabbar correctly (Icons and titles) but fails to show e.g. OneViewController's view. I assume that the view is not loaded since the - (void)viewDidLoad is not being called for any of the subview controllers.
I would appreciate any suggestions.
Thanks,
equi
Are you sure your nib names are how you have typed them? It's not called OneViewController.xib?, for example?
I sorted out the issue.
The above code actually worked, reason for the view not appearing was that I accidentally synthesised 'view' in the UIViewController derivative.
I'm creating an application that, when the user selects a project in a table, displays a view with a tab bar at the bottom. I'm using this code:
- (id)init
{
[super initWithNibName:nil bundle:nil];
tabController = [[UITabBarController alloc] init];
// Create all view controllers to be included in the tab bar
SummaryViewController *summaryController = [[SummaryViewController alloc] init];
ImagesViewController *imagesController = [[ImagesViewController alloc] init];
// Make an array containing the view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:summaryController, imagesController, nil];
[summaryController release];
[imagesController release];
// Attach them to the tab bar controller
[tabController setViewControllers:viewControllers];
[self.view addSubview:tabController.view];
return self;
}
With the end result being the tab bar at the bottom appearing cut off around half its height. I'm guessing it's somehow related to the navigation controller at the top (link to image).
Any help in solving this mystery would be greatly appreciated!
I believe the problem here is that you're adding the UITabBarController as a subview of another UIViewController. UITabBarController descends directly from UIViewController.
Instead of creating this view controller to hold it, you should just create the tab bar controller, configure it and push that onto your stack.