Navigation controller not displaying back button w

2019-08-02 08:29发布

I'm doing a navigation based application. Let me explain a little bit about the application. At the program load table view will show 3 options like:

  • Hotel
  • Restaurants
  • Cafe

If a user selects any of the above options, it will navigate to another table view and show sub options. And it'll continue up-to 6 to 7 levels. So I in the didSelectRowAtIndexPath: of the tableView's class (Sales) I created it's own object and using [self.navigationController pushViewController:myViewController animated:YES]; I'm creating another view and binds data from the database. It's working fine for me, ut the issue is. I'm not getting the 'back' button that is automaticaly displays on the navigation bar.

my implementation is like this

@implementation Sales

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  {
 Sales *myViewController  = [[Sales alloc] initWithNibName:@"Sales"];
 [self.navigationController pushViewController:myViewController animated:YES];
 //Do other stuffs here
}

@end

Like this I'm displaying the all levels. But when I navigated inside of any option I didn't get a back button. (Usually if I navigates to Hotel it will show a detailView and shows a back button on the navigation bar, but in this case this is not happening). I think the issue is with creating objects of the same class and displaying it, but I can't create such a number of classes and views (about 43 levels total). How can I resolve this issue (How can I show back button on the navigation bar) ? Is my method is good one or Is there any alternative solutions for this issue? Thanks in advance.

2条回答
一夜七次
2楼-- · 2019-08-02 08:48

When you create a "Back" button, it's for the "next" level. Therefore the back button of the "current navigation controller" was created at the previous level... Yes, it's a bit confusing.

Well, I blogged about this at "iLessons iLearned: How to Define Custom backBarButtonItem", but here's sample code for back button creation:

- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *backButton =
  [[UIBarButtonItem alloc]
    initWithTitle:@"My Back"
    style:UIBarButtonItemStyleBordered
    target:nil
    action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
}

Now just make sure you use it at correct navigationController!

查看更多
Viruses.
3楼-- · 2019-08-02 09:02

Your method to push a view controller of the same class is fine. The error must be elsewhere.

  • Check if the view controller has a title. That is the text that is shown on the back button when the child view controller is visible. If that text is nil there will be no back button.
查看更多
登录 后发表回答