UINavigationController 上的后退按钮

发布于 2024-09-25 09:52:46 字数 441 浏览 5 评论 0原文

我知道这看起来很奇怪,但我需要在第一个 navigationController 视图的导航栏上添加一个后退按钮。我尝试这样:

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

如果我写 leftBarButtonItem 而不是 backBarButtonItem 则显示按钮。我的问题是我需要一个箭头按钮作为正常的后退按钮。这可能吗?

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. 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. My problem is that i need an arrow button as the normal back button. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

£冰雨忧蓝° 2024-10-02 09:52:47

通常这可以开箱即用,但有时对于模式视图/操作表,您可能需要它。在实例化视图控制器并将其推送到导航控制器堆栈之前,尝试

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];

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

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];
丘比特射中我 2024-10-02 09:52:47

我认为您不能在第一个 NavigationController 视图上执行此操作,因为您需要在推送子控制器之前在父控制器中设置 backBarButtonItem 属性。另外,根据 Apple 文档< /a>,目标& backBarButtonItem 的操作必须为 nil

关于创建左箭头的问题UIToolbar 上的按钮 可能会给您一些关于如何使用自定义图像(对于 leftBarButtonItem)解决此问题的想法。

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. Also, according the to the Apple docs, the target & action of the backBarButtonItem must be 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).

童话里做英雄 2024-10-02 09:52:47

或者你也可以执行以下操作 - 我更喜欢这种方法。我从另一个帖子得到这个。

使用我从 http://www.teehanlax.com/blog/?p= 导出的以下 psd第447章

http://www.chrisandtennille.com/pictures/backbutton.psd< /p>

然后我创建我在工具栏项的 customView 属性中使用的自定义 UIView。

对我来说效果很好。

希望有一点帮助

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.chrisandtennille.com/pictures/backbutton.psd

Then I just create a custom UIView that I use in the customView property of the toolbar item.

Works well for me.

Hope that helps a little

在巴黎塔顶看东京樱花 2024-10-02 09:52:47

当然你可以这样做。您只需将 leftBarButtonItem 的标题更改为 back
然后你会得到一个漂亮的左箭头按钮,标题又回来了。然后,您只需更改选择器即可在单击按钮时实际执行方法。所以 @selector(foo:)

这里有一些关于如何实现上述目标的代码:

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

如果有帮助,请告诉我。

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. Then you just change the selector to actually perform a method when the button is clicked. So @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.

可是我不能没有你 2024-10-02 09:52:47

Apple 文档说:

当此导航项紧邻堆栈中的顶部项下方时,导航控制器将从该导航项派生导航栏的后退按钮。

因此,如果您的导航项是堆栈的顶部(正如我们在这里所说的),您无法将后退按钮添加到导航控制器,只是因为他无法导航回它,因为它是堆栈中的顶部项。

更新答案:
在我搜索之后,我发现可以在导航控制器的根视图控制器中制作一个后退按钮,这些 链接

非常简单:)

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

self.navigationItem.leftBarButtonItem = backButton;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文