简体   繁体   English

UINavigationController上的“后退”按钮

[英]Back button on UINavigationController

I know that it could seem strange but i need to add a back button on the navigation Bar of the first navigationController's view. 我知道这似乎很奇怪,但是我需要在第一个navigationController视图的导航栏上添加一个后退按钮。 I tried like this: 我这样尝试过:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Foo" style:UIBarButtonItemStyleBordered target:self                                                          action:@selector(foo:)]; 
self.navigationItem.backBarButtonItem=backButton;

if instead of backBarButtonItem i write leftBarButtonItem the button is showed. 如果代替backBarButtonItem,我编写leftBarButtonItem,则显示按钮。 My problem is that i need an arrow button as the normal back button. 我的问题是我需要一个箭头按钮作为普通的后退按钮。 Is this possible? 这可能吗?

Usually this works out of the box, but sometimes with modal views / action sheets you may need this. 通常,这是开箱即用的,但有时在模式视图/操作表中可能需要此功能。 Just before you instantiate your viewcontroller and push it onto navigationcontroller stack, try 在实例化ViewController并将其推入NavigationController堆栈之前,请尝试

UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Back" style: UIBarButtonItemStyleBordered target: nil action: nil];
    [[self navigationItem] setBackBarButtonItem: newBackButton];
    [newBackButton release];

    DetailViewController *detailVC = [[DetailViewController alloc]init];
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];

Apple Document says: 苹果文档说:

When this navigation item is immediately below the top item in the stack , the navigation controller derives the back button for the navigation bar from this navigation item. 当此导航项目位于堆栈顶部项目的下方时 ,导航控制器将从该导航项目派生导航栏的后退按钮。

So If your navigation item is the top of the Stack (as we are talking here) you can't add the back button to the navigation controller, simply because no place he can navigate back to it because it's the top item in the stack. 因此,如果您的导航项位于堆栈的顶部(如我们在此处所述),则不能将后退按钮添加到导航控制器,这仅仅是因为他没有位置可以导航回它,因为它是堆栈中的顶部项。

Updated Answer : After I searched I found work a round to make a back button in your root view controller in Navigation controller in these link 更新答案:搜索后,我发现在这些链接中的导航控制器的根视图控制器中做了一个回退按钮

It's very simple :) 这很简单:)

[self.navigationItem setHidesBackButton:YES animated:YES];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Start" style:UIBarButtonItemStyleBordered target:self action:@selector(initializeStuff)];

self.navigationItem.leftBarButtonItem = backButton;

I don't think you can do that on the first NavigationController view, because you need to set the backBarButtonItem property in the parent controller, before the child controller is pushed. 我认为您无法在第一个NavigationController视图上执行此操作,因为您需要在backBarButtonItem子控制器之前在控制器中设置backBarButtonItem属性。 Also, according the to the Apple docs , the target & action of the backBarButtonItem must be nil . 另外,根据Apple文档backBarButtonItem的target和action必须为nil

This question about creating a left-arrow button on a UIToolbar may give you some ideas of how you could work around this using a custom image (for the leftBarButtonItem). 有关在UIToolbar上创建左箭头按钮的问题,可能会给您一些有关如何使用自定义图像(针对leftBarButtonItem)解决此问题的想法。

Of course you can do this. 当然可以。 You just need to change the leftBarButtonItem's title to back then you will get a nice left arrow button with the title back. 您只需要将leftBarButtonItem的标题更改为back即可,然后您将获得一个漂亮的向左箭头按钮,标题为back。 Then you just change the selector to actually perform a method when the button is clicked. 然后,只需更改选择器即可在单击按钮时实际执行一种方法。 So @selector(foo:) 所以@selector(foo:)

Here some code on how to achieve the above: 这里有一些关于如何实现以上的代码:

self.navigationItem.leftBarButtonItem.style = UIBarButtonItemStyleDone;
self.navigationItem.leftBarButtonItem.title = @"Back";
self.navigationItem.leftBarButtonItem.target = self;
self.navigationItem.leftBarButtonItem.action = @selector(endTextEnteringButtonAction:);

Let me know if that helps. 让我知道是否有帮助。

or you could also do the following - I prefer this method. 或者您也可以执行以下操作-我更喜欢这种方法。 I got this from a different post. 我是从其他帖子中得到的。

Use following psd that I derived from http://www.teehanlax.com/blog/?p=447 使用我从http://www.teehanlax.com/blog/?p=447导出的以下psd

http://www.chrisandtennille.com/pictures/backbutton.psd http://www.chrisandtennille.com/pictures/backbutton.psd

Then I just create a custom UIView that I use in the customView property of the toolbar item. 然后,我只创建一个用于工具栏项的customView属性的自定义UIView。

Works well for me. 对我来说效果很好。

Hope that helps a little 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM