开发者

why won't the back button appear given this code (UINavigationController question)

开发者 https://www.devze.com 2023-02-13 12:16 出处:网络
Can\'t seem to get the Back Button to appear in a UINavigationController flow.I just want it to trigger the pop\'ing of the current controller automatically to get back to the parent.

Can't seem to get the Back Button to appear in a UINavigationController flow. I just want it to trigger the pop'ing of the current controller automatically to get back to the parent.

I'm getting a little confused in terms of what the minimum necessary is to get an automatic back button (I mean the one with the title of the parent controller and a button that has an arrow pointing back to the left), and what you then need to do to custom things beyond this. I'm just searching for the former at this stage.

@implementation AppointmentListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Detailed View";
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:nil];
    self.navigationItem.backBarButtonItem = backButton;
    [backButton release];
}

The code to get to this view (i.e. code in parent controller):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     AppointmentListController *appointmentListController = [[AppointmentListController alloc] initWithNibName:@"AppointmentListController" bundle:nil];
     [ [self navigationController] pushViewController:appointmentListController animated:YES];
     [appointmentListController release];
}

EDIT: PS with the code above I actually don't see any button at all appear on the left.

EDIT2: David, I tried button the following backbutton code in the RootViewController (and pulled it out of the AppointmentListController), however it still doesn't show any sort of back button?

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];

EDIT3: David - Did work actually with the following code, except that the text was "back" and not the title of the parent controller like I'm after - tried deleting the .title= line but then no button appeared.

// create a custom navigation bar button and set it to always say "Back"
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
temporaryBarButtonItem.title = @"Back";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[temporaryBarButtonItem release];

EDIT4 - by bad - I hadn't set the title of the parent controller so I guess in this case it doesn't try to but a back button up at all - so adding "self.title = @"Views";" to the parent controller fixed things (and removing the line where I was manually setting the back button tittle)

EDIT5 - for anyone who was confused like me - so in the end you don't need any of the backBarButtonItem setting code in the Parent controller at all - just had 开发者_JS百科to make sure the parent controller had a "title" set and then things seem to work automatically - doh


The back button refers not to the top controller, but the second-of-controller stack controller. You put the back button on your detailViewController. You should have put it on your UITableViewController.


The backBarButtonItem is what is displayed when the view controller is behind another view controller in a navigation stack. In other words, if you were to push a view controller on top of it, instead of seeing "Detailed View" in the back button, it would be a cancel button.

To get the behavior you want, use leftBarButtonItem

self.navigationItem.leftBarButtonItem = backButton;


At the time view did lOad is called the appropriate navigationbar is not On the stack, you should write the code in the viewdidappear method of UIViewController


The back button appears automatic if you set your viewController title first (self.title).

Then if you push a view controller from a view controller having title "first view", the pushed one will has a back button with the label "first view".

If you don't set the title the automatic back button appear only on the third pushed view, not before.

firstController (title: "", back: NO) -> secondController (title: "", back: NO) -> thirdController (title: "", back: YES)

I don't know if it's a bug or they wanted it. I read the UINavigationController guide lines but doesn't see nothing about it.

0

精彩评论

暂无评论...
验证码 换一张
取 消