Back Navigation Button Not Showing up in Pushed View Controller
I have a problem where I can successfully push a new view controller from my root view controller (using the Navigation-based application using开发者_开发问答 Core Data template), but the detail view, which is a separate xib file from the root view controller, doesn't display the back navigation button. I'm certain that I've done all of the proper connections in IB, and everything else is working as expected.
RootViewController.h
@class ItemsViewController;
@interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate> {
IBOutlet ItemsViewController *itemsVC;
}
@property (nonatomic, retain) IBOutlet ItemsViewController *itemsVC;
@end
RootViewController.m
#import "ItemsViewController.h"
@synthesize itemsVC;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Pushes to ItemsViewController
ItemsViewController *itemsViewController = [[ItemsViewController alloc] initWithNibName:@"ItemsViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:itemsViewController animated:YES];
[itemsViewController release];
}
ItemsViewController is a UITableViewController subclass with its own xib, also named ItemsViewController. Again, it gets pushed from RootViewController, but the back button doesn't show up like this. I was under the impression that it was a "free" feature of using a navigation controller. As you might expect, this is a very frustrating roadblock and I would appreciate any help.
Does your ItemsViewController
class set its title
property in its viewDidLoad
method?
You should call [tableView deselectRowAtIndexPath:indexPath animated:YES]
as the last line of tableView:didSelectRowAtIndexPath:
to conform to Apple's human interface guidelines.
Yeah, make sure you have a title on your RootViewController, if it doesn't no button will appear. To set a title programmatically;
self.navigationItem.title = @"Hello Der";
Some extra info for future readers: I ran into this problem while trying to re-populate a nav hierarchy on application load, so I was calling setViewControllers: from the app delegate's application:didFinishLaunchingWithOptions: method. And I was running into the same problem with the back button not showing up.
In my case, setting the root view controller's nav item title in its viewDidLoad did not work, presumably because the other views were loading first and seeing the root's title as null. Instead, I set the root view controller's nav item title in my application:didFinishLaunchingWithOptions: method right before I called setViewControllers:, and that fixed it.
This can also happen if you're a bonehead and mistakenly initialize the view as modal instead of pushing a view to the navigation controller.
oops: [self presentModalViewController:navController animated:YES]; meant: [self.navigationController pushViewController:controller animated:YES];
Try following snippet. It work for me!
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Image.png"] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar bringSubviewToFront:[self.navigationController.navigationItem backBarButtonItem]];
精彩评论