如何在导航栏中添加右键? [英] How to add right button in the navigation bar?

查看:83
本文介绍了如何在导航栏中添加右键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题要在导航栏中添加一个右键..

I have a question to add a right button in navigation bar..

我有两个观点:查看A和查看B

I have two views: View A and View B

在我使用 self.navigationController!.pushViewController 显示视图B后,我添加了一个导航栏来查看A.

I add a navigation bar to view A, after I used self.navigationController!.pushViewController to show view B.

在自动导航栏左侧显示一个后退按钮,很不错。但现在我想在视图B的导航栏右侧添加一个按钮..
我已经尝试了一些答案,但它不起作用...
我试过的答案喜欢:
1) https://www.hackingwithswift.com/example-code/uikit/how-to-add-a-bar-button-to-a-navigation-bar
2) http://blog.apoorvmote.com/add-multiple- bar-button-item-navigation-bar /?lang = fr

That show a back button in the left of navigation bar of view B automatic, it is good. but now I want to add a button in the right of navigation bar of view B.. I have tried some answers, but it doesn't work... I have tried answers likes : 1) https://www.hackingwithswift.com/example-code/uikit/how-to-add-a-bar-button-to-a-navigation-bar 2)http://blog.apoorvmote.com/add-multiple-bar-button-item-navigation-bar/?lang=fr

你能帮助我吗,谢谢!

推荐答案

当您使用这种语言时,Vahan Babayan的答案的Swift版本是:

The Swift version of Vahan Babayan's answer, as you seem to use this language, is:

let rightButtonItem = UIBarButtonItem.init(
      title: "Title", 
      style: .Done, 
      target: self, 
      action: "rightButtonAction:"
)

self.navigationItem.rightBarButtonItem = rightButtonItem

将在self上调用以下方法:

The following method will be called on self:

func rightButtonAction(sender: UIBarButtonItem)

请注意,所有这些都可以使用Storyboard 以图形方式设置,方法是将条形按钮项拖动到导航项并右键单击以设置目标操作。

Note that all this can be set graphically using a Storyboard, by dragging a Bar Button Item to your Navigation Item and right-clicking to set a target-action.

自Swift 3和4以来的一个小更新:编译器现在可以检查选择器名称,以编程方式设置目标操作时防止打字错误。所以应该真正使用:

A small update since Swift 3 and 4 are out: the compiler can now check selector names, preventing typos when setting up target-action programatically. So one should really use:

let rightButtonItem = UIBarButtonItem.init(
      title: "Title", 
      style: .Done, 
      target: self, 
      action: #selector(rightButtonAction(sender:))
)

这篇关于如何在导航栏中添加右键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆