I have a Tab Bar application with 6 Tab Bar items that each open a UITableView. I am trying to enable each table with the ability to open a Detail View Controller when an item in a row on the table is selected.
For example, for the first ViewController ( ViewController1.m ), I created
DetailView1.xib
DetailViewController1.h
DetailViewController1.m
In order to get each row in ViewController1.m 's TableView, I understand I must
use this method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
This is my code for that method, which does not produce any errors or warnings, but nothing sees to happen when the TableViewCell is selected:
DetailViewController1 *dvController = [[DetailViewController1 alloc] initWithNibName:#"DetailView1" bundle:[NSBundle mainBundle]];
[navController pushViewController:dvController animated:YES];
[dvController release];
Should this not load DetailView1.xib? I created this with the Tab Bar Application Template...which has no NavigationController in it by default I believe. Is it possible something is not hooked up right in Interface Builder?
You'd need to configure each tab to contain an instance of UINavigationController with one of your view controllers nested inside of it. Then in tableView:didSelectRowAtIndexPath:, you'd want to change the second line of code in your example to this:
[[self navigationController] pushViewController:dvController animated:YES];
Related
i have a tabBarController with 3 items.
In my first view in my first item of my tabBar i have a tableview and a navigationController.
how to go in the second item of my tabBar when i push 1 row in my tableview in the fist item of my tabBar?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Second *two = [[Second alloc] initWithNibName:#"Second" bundle:nil];
[self.navigationController pushViewController:two animated:YES];
[two release];
thks
Use the selectedIndex property on the UITabBarController. More information can be found in the UITabBarController Class Reference.
self.tabBarController.selectedIndex = 1;
or use
[self.tabBarController setSelectedIndex:1];
Note that the indices start at 0, so the second tab is at index 1.
Its not a good practice to automatically select a tabBarItem when a view controller is pushed into a navigation controller stack of another tabBarItem. If you want the Second view to be in second tab, you should add that view controller to the second tab.
I'm currently trying to spawn a new instance of the same view - using the following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
InventoryController *inventoryController = [[InventoryController alloc] initWithNibName:#"InventoryView" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self];
[navigationController pushViewController:inventoryController animated:YES];
[inventoryController release];
[navigationController release];
}
Problem is that it's not working...
I don't get any errors or anything - it just doesn't do anything.
Any ideas?
#PengOne has it right... you're creating a navigation controller and then releasing it, and there's nothing to prevent it from being deallocated. Additionally, you haven't added the nav controller's view to the window, and you haven't set the nav controller as the window's root view controller, so there's no way for the views controlled by controllers in this particular navigation stack to ever be seen.
Try this: Create a navigation-based project in Xcode. You don't need to add any code -- just create the project so that you can look at the code that's provided. You'll see that the app delegate has a retain property for storing the nav controller, and the nav controller is set as the window's root view controller.
If your current controller is already a part of UINavigationController hierarchy then you must not create a new navigation controller - use the existing one instead (note that every UIViewController has a reference to its parent UINavigationViewController if it exists):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
InventoryController *inventoryController = [[InventoryController alloc] initWithNibName:#"InventoryView" bundle:nil];
[self.navigationController pushViewController:inventoryController animated:YES];
[inventoryController release];
}
I created iOS application from Navigation-based application template.
This snippet is from RootViewController.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DotoViewController *detailViewController = [[DotoViewController alloc]
initWithNibName:#"DotoViewController" bundle:nil];
NSManagedObject *selectedObject = [[self fetchedResultsController]
objectAtIndexPath:indexPath];
detailViewController.dotoObj = selectedObject;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
DotoViewController is a subclass of UIViewController and has an accompanying NIB file.
Upong clicking on a row, DotoViewController is getting displayed but without the back button to RootViewController on the left side.
What I'm missing?
Perhaps your root view controller has an empty title? The view controller's title is used as the title for the back button, so if you have an empty string there, this can cause the back button to disappear. If it's nil, it should display the standard "Back" though.
I just filled my UITableView with Planets. I would like each cell clicked to open into a new Xib (if this sounds like the wrong approach please direct). I can get a secondviewcontroller working, its getting the thirdviewcontroller and fourthviewcontroller working? Thanks.
Place your main view controller (the one with the table) inside a UINavigationController. Then, when the user selects a row, push a new view controller onto it.
The following function will help. As Ben Gottlieb said your main view controller will need to be in a UINavigationController. You need to implement the delegate method for didSelectRowAtIndexPath and this is where you create the new controller for your new view and load it.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
YourViewController *controller = [[YourViewController alloc] initWithNibName:#"YourViewController"bundle:nil];
[[self navigationController] pushViewController:yourViewController animated:YES];
[yourViewController release]; // don't leak memory
}
Based on the row number you can decide which nib to load.
I recently found a good tutorial about how to place a navigation controller within a tabbarcontroller("The nib way").
http://twilloapp.blogspot.com/2009/05/how-to-embed-navigation-controller.html
I continued with the second step and added a tableviewcontroller to the navcontroller.
What I don't understand, is how I can use the navigationbarcontroller from within my tableviewcontroller and e.g - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
What I want to do is when a user selects a row another view should slide in with the back button and everything that a navigationcontroller provides.
Thanks in advance!
Have you started with Apple's SimpleDrillDown sample? The specific code in question is this routine:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
Create the detail view controller and set its inspected item to the currently-selected item
*/
DetailViewController *detailViewController = [[DetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
detailViewController.detailItem = [dataController objectInListAtIndex:indexPath.row];
// Push the detail view controller
[[self navigationController] pushViewController:detailViewController animated:YES];
[detailViewController release];
}
The following was what I was looking for:
[[self navigationController] pushViewController:detailViewController animated:YES];
[detailViewController release];
Since I've specified the RootView Controller for my NavigationControllers View, self answers to navigationController.