如何使用 Xcode 在导航栏中添加共享按钮的操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24991713/
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 an action to share button in navigation bar with Xcode
提问by Moussa
I am trying to add an action to my share button in navigation bar but I don't know how and where to define my "shareAction" method. To add share button, I have the following code in viewWillAppear :
我正在尝试向导航栏中的共享按钮添加一个操作,但我不知道如何以及在何处定义我的“shareAction”方法。要添加共享按钮,我在 viewWillAppear 中有以下代码:
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:@selector(shareAction:)];
self.navigationItem.rightBarButtonItem = shareButton;
回答by Kathiravan G
in your implementation.m file
在您的 implementation.m 文件中
- (void) viewWillAppear
{
[super viewWillAppear:animated];
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:@selector(shareAction:)];
self.navigationItem.rightBarButtonItem = shareButton;
}
-(void)shareAction:(id)sender
{
NSLog(@"share action");
}
回答by Bobj-C
Swift
迅速
let shareBar: UIBarButtonItem = UIBarButtonItem.init(barButtonSystemItem:.Action, target: self, action: Selector("userDidTapShare"))
self.navigationItem.rightBarButtonItem = shareBar
func userDidTapShare() {
//Implementation goes here ...
}
回答by Piyush Dubey
In Swift 3.0
在 Swift 3.0 中
Write this code in viewDidLoad
把这段代码写在 viewDidLoad
let btnShare = UIBarButtonItem(barButtonSystemItem:.action, target: self, action: #selector(btnShare_clicked))
self.navigationItem.rightBarButtonItem = btnShare
Share button action
分享按钮动作
func btnShare_clicked() {
print("Share button clicked")
}