iOS table view navigation controller with multiple views - iphone

I have an iOS 7 application that has a Tab Bar controller. On one tab I have a tableView, from which I can select a cell, and navigate to a detail view.
On the detail view I had the following to be able to navigate to another view:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Table View Cell Clicked on row: %ld", indexPath.row + 1);
ThirdViewController *view = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:[NSBundle mainBundle]];
modelView.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:view animated:YES];
}
But this does not let me navigate to the new View. How can I navigate to multiple view, I require this:
View1 -> View2 -> View3 -> View4 -> View5 -> View6 - All these view contain a table view, and the selection to another view is by selecting on row on the table.
Thanks
Best Regards
UPDATED:
Problem solved. the last table view did to add the delegate outlet set. That wa the problem. Thanks

Check if self.navigationController is nil. If it is, it means you need to add navigation controller to the tab view controller:
[tabViewControllers addObject:[[UINavigationController alloc] initWithRootViewController:myTableViewController]];
tabBarController.viewControllers = tabViewControllers;

Related

iOS pushing view controller not working

I've got a uitabbarcontroller set as the root view of the window. One of the tabs in the tabbar is set to a subclass of uitableviewcontroller. What I'm trying to do is show a details view when one of the rows is touched so I implemented
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!detailViewController) {
detailViewController = [[ItemDetailViewController alloc] init];
}
[detailViewController setEditingArray:
[list objectAtIndex:[indexPath row]]];
[[self navigationController] pushViewController:detailViewController animated:YES];
}
in my table controller, but when I select the row, my detailview doesn't pop in. I added nslog to see if the function fires - and it does, but my detailview still doesn't show. Any ideas?
Thanks.
The tableViewController should be placed inside a NavigationController.
I guess you created tableView inside a tab bar.
TableViewControllerSubclass* table = [[TableViewControllerSubclass alloc] init];
UINavigationController* tableNav = [[UINavigationController alloc] initWithRootViewController:table];
mainController = [[UITabBarController alloc] init];
mainController.viewControllers = [NSArray arrayWithObjects:tableNav,..., nil];
You don't have a navigation controller ([self navigationController] is returning nil). Instead of having the table view controller as the root of the tab, make the tab's root a navigation controller containing the table view controller. You will now be able to push the detail controller from the table view controller.

NavigationController on TabBarItem

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.

Changing Views with MGSplitView Controller

In the app that io am creating, i have a custom copy of UISplitView Controller, MGSplitViewController. I have implemented it into my project which started off with the MultipleDetailViews sample code from apple.
I have come across a problem where i cant seem to switch between viewcontrollers. When i push the tableview cells, the detailview controller should change according the the nib assigned, however that doesn't happen.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
/*
Create and configure a new detail view controller appropriate for the selection.
*/
NSUInteger row = indexPath.row;
UIViewController *detailViewController = nil;
if (row == 0) {
FirstDetailViewController *newDetailViewController = [[FirstDetailViewController alloc] initWithNibName:#"FirstDetailView" bundle:nil];
detailViewController = newDetailViewController;
}
if (row == 1) {
SecondDetailViewController *newDetailViewController = [[SecondDetailViewController alloc] initWithNibName:#"SecondDetailView" bundle:nil];
detailViewController = newDetailViewController;
}
// Update the split view controller's view controllers array.
NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
splitViewController.viewControllers = viewControllers;
[viewControllers release];
[detailViewController release];
Normally this code would be enough to change the views in the original Multiple Detail View code.
has anyone run into a similar problem? any ideas?
You're just creating new view controllers. You're not adding them anywhere. You add view controllers to the split view controller by using its viewControllers property.
EDIT: I've used MGSplitViewController, but I never tried to change the detail view like that. I just pushed the new detail view controller onto the navigation controller. Is there a specific reason for wanting to change the detail view controller entirely?

Access a new UIViewController from a UITableViewCell

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];

How to open a new UITableView when clicked on a table cell and add navigation controller dynamically

Basically I add a subview to the main window. This subview has a search bar and a table view. The search bar works and it displays some data on the table view cells. Now when I click on a cell it should load another view with a navigation bar (w/ back button) and a table inside that view. How can this be done? I tried the following code and it doesn't work, it gives me an error saying - "'-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "DetailViewController" nib but the view outlet was not set.'
I tried to set the outlet's in the DetailViewController.nib file but it just wouldn't let me.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[UITableViewController alloc]initWithNibName:#"DetailViewController" bundle:nil]];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
}
Basically you push the detail view controller onto the navigation controller.
I published a sample code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailContactViewController *detailViewController = [[DetailContactViewController alloc] initWithNibName:#"DetailContactView" bundle:nil];
detailViewController.contact = [contacts objectAtIndex:indexPath.row];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}