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)?
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];
.
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.
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.
精彩评论