开发者

How do I change the back button on a UINavigationControler

In the view I want to change it for I have the following code but it fails.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //Logout button
    UIBarButtonItem * logout = 开发者_如何学Go[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(goBack)];
    logout.title = @"Logout";
    nav_delegate.navigationController.navigationItem.leftBarButtonItem = logout;
    [logout release];
}

Thank you for any help.


Implementing backBarButtonItem is for the super view controller which uses pushViewController:subViewController.

For example, if you've pushed a view controller for its super view controller Logout:

[self.navigationController pushViewController:subViewController animated:YES];

Then, you should've implemented backBarButtonItem in the super view, which is Logout view, NOT in the pushed subViewController.

So, to implement backBarButtonItem, you do it in super view Logout, like:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:logoutViewTitle style:UIBarButtonItemStyleBordered target:nil action:nil];

You can do this in -(void)viewDidLoad for static use, or in -(void)viewWillAppear:(BOOL)animated for dynamic use, for setting the title without allocating and initializing.

One more tip: In interface builder, there is input field for backBarButtonItem title. But if you didn't enter, you must allocate and initialize the backBarButton with title in .m files, like the code above. If you've entered the title for static use, I believe you can change it simply by using:

[self.navigationItem.backBarButtonItem setTitle:logoutViewTitle];

Hope it helped.


Set the backBarButtonItem on the previous view controller (the one that you will return to when you press the back button).


Here's the answer. In the view controller:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"X";
    //Logout button
    UIBarButtonItem * logout = [[UIBarButtonItem alloc] initWithTitle:@"Logout" style: UIBarButtonItemStylePlain target:self action:@selector(goBack)];
    self.navigationItem.leftBarButtonItem = logout;
    [logout release];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜