ios 如何在导航栏中添加右键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36693678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add right button in the navigation bar?
提问by user2262304
I have a question to add a right button in navigation bar..
我有一个问题要在导航栏中添加一个右键按钮..
I have two views: View A and View B
我有两个视图:视图 A 和视图 B
I add a navigation bar to view A, after I used self.navigationController!.pushViewController
to show view B.
在我用来self.navigationController!.pushViewController
显示视图 B之后,我添加了一个导航栏来显示视图 A。
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-bar2)http://blog.apoorvmote.com/add-multiple-bar-button-item-navigation-bar/?lang=fr
那个自动在视图B的导航栏左边显示一个后退按钮,很好。但现在我想在视图 B 的导航栏右侧添加一个按钮..我尝试了一些答案,但它不起作用......我尝试过以下答案:1)https://www.hackingwithswift。 com/example-code/uikit/how-to-add-a-bar-button-to-a-navigation-bar2) http://blog.apoorvmote.com/add-multiple-bar-button-item-navigation -bar/?lang=fr
Could you help me, thank you !
能不能帮帮我,谢谢!
回答by Jean-Baptiste
The Swift version of Vahan Babayan's answer, as you seem to use this language, is:
Vahan Babayan 的答案的 Swift 版本,正如您似乎使用这种语言一样,是:
let rightButtonItem = UIBarButtonItem.init(
title: "Title",
style: .Done,
target: self,
action: "rightButtonAction:"
)
self.navigationItem.rightBarButtonItem = rightButtonItem
The following method will be called on self:
将在 self 上调用以下方法:
func rightButtonAction(sender: UIBarButtonItem)
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.
请注意,所有这些都可以使用 Storyboard以图形方式设置,方法是将 Bar Button Item 拖动到您的 Navigation Item 并右键单击以设置目标操作。
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:
Swift 3 和 4 发布后的一个小更新:编译器现在可以检查选择器名称,防止在以编程方式设置目标操作时出现拼写错误。所以一个人真的应该使用:
let rightButtonItem = UIBarButtonItem.init(
title: "Title",
style: .Done,
target: self,
action: #selector(rightButtonAction(sender:))
)
回答by Vahan Babayan
You can add the following code to your B controller's viewDidLoad
method.
您可以将以下代码添加到 B 控制器的viewDidLoad
方法中。
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Title"
style:UIBarButtonItemStyleDone
target:self
action:@selector(rightButtonAction:)];
self.navigationItem.rightBarButtonItem = rightButtonItem;
回答by Vasanthan Prem
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"Right Button" style:UIBarButtonItemStyleDone target:self action:@selector(rightBtnClick)];
self.navigationItem.rightBarButtonItem = rightBtn;
}
-(void)rightBtnClick{
// code for right Button
}
回答by iPhoneDeveloper
Swift code to add a navigation bar button item:
添加导航栏按钮项的Swift代码:
Method 1(When you wanna use bar button system item)
方法一(当你想使用条形按钮系统项时)
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
Method 2(When you wanna give your own title to button)
方法 2(当你想给自己的按钮标题时)
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
From iOS 5 onwards, it allows you to add more than 1 button on either side of a navigation bar. Something like this:
从 iOS 5 开始,它允许您在导航栏的任一侧添加 1 个以上的按钮。像这样的东西:
let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
let display = UIBarButtonItem(title: "Display", style: .plain, target: self, action: #selector(playTapped))
navigationItem.rightBarButtonItems = [add, display]
回答by Paras vora
Add below code in viewDidLoad Method
在 viewDidLoad 方法中添加以下代码
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:@"Flip"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(flipView:)];
self.navigationItem.rightBarButtonItem = flipButton;
This adds a button to the right hand side with the title Flip, which calls the method:
这会在右侧添加一个标题为 Flip 的按钮,该按钮调用该方法:
-(IBAction)flipView