views:

273

answers:

3

I'm working on a view-based iPhone app that has the following flow:

search -> (list | map) -> details

To accomplish the transitions between views, I have a UINavigationController, with the search view controller being the root. After a user performs a search, I transition to a view with a segmented control, which acts as a tab to switch between the list and map functionality (per a suggestion from a related question I had). This view contains a UIViewController, which allows me to switch between the list/map view when a user taps the segmented control. I'm fine up until this point. In other words, I am able to transition from the search view into a SwitchViewController, which is responsible for allowing me to switch between the list and map views. From the SwitchViewController, I'm also able to transition back to the search view.

As you can see from the above mentioned flow, I would like to provide the ability to transition into a details view. Each row of my table in the list view contains a details disclosure button for allowing the user to drill down into a details view. The problem is, when I try to push the details view onto the navigation stack, nothing happens.

Below is the delegate method (from my list view controller) to handle the details disclosure button being tapped. I've set up break points, so I know the code is running. The navigation controller simply doesn't want to push the detailsController onto the stack (my guess is that I don't have a pointer to the correct UINavigationController).

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
 if (detailController == nil)
 {
  detailController = [[DetailsViewController alloc] init];
 }

 [self.navigationController pushViewController:detailController animated:YES];
}

Assuming I was probably missing a pointer to the navigation controller, I exposed a UINavigation property on my list and map views (navigationController is readonly) and initialized them with a pointer to the navigation controller from my SwitchViewController (the view responsible for switching between list/map views when a user changes the value of the segmented control). Unfortunately, this did not solve the problem.

Am I on the right track? If so, how do I see to it that my view has a pointer to the correct navigation controller? Should I add a delegate, which allows me to call a function in the SwitchViewController that transitions into the details view (this seems messy)?

+1  A: 

Are you SURE that you have a navigation controller? In your code put NSLog(@"%@",self.nagivationController);. If it comes up NULL then you are in trouble.

Also, try [self.parentViewController.navigationController pushViewController:detailController animated:YES]; .

Pyro2927
Thank you for the response. I apologize for the delay replying. When I try this, I get the following output: 2010-05-07 11:25:44.861 MyAppName[2095:207] (null). I know I have a navigation controller because I'm using it to transition from the search view to the list/map view. I also tried accessing the parents navigation controller. Unfortunately, it didn't do the trick.
senfo
+1  A: 

To implement navigation controller in view based application.

Put this code

In delegate .h class

      MyViewController *viewController;

In delegate .m class

 - (void)applicationDidFinishLaunching:(UIApplication *)application {    

UINavigationController *nvcontrol =[[UINavigationController alloc] initWithRootViewController:viewController];

[window addSubview:nvcontrol.view];

[window makeKeyAndVisible];

}

Here "MyViewController" should be replaced by your viewcontroller.

After implementing this in you delegate class,your code will work.

All The Best.

Warrior
Just to make sure I'm following you correctly. I added a Navigation Controller to my MainWindow.xib file through Interface Builder, where I also configured the root nib. I believe what you're suggesting here is that I simply do the same thing, but in code. Is that correct? It's important to note that the navigation controller works up until the point that I reach the SwitchViewController (the view, which allows me to switch between the list and map views).
senfo
A: 

I ended up simply creating delegates on the list and map views, which I use to call a method on the SwitchViewController to switch the view. I was hoping to find a cleaner way, but this did the trick.

senfo