我正在尝试向导航栏中的共享按钮添加一个操作,但我不知道如何以及在哪里定义我的“shareAction”方法。要添加共享按钮,我在 viewWillAppear 中有以下代码:
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:@selector(shareAction:)];
self.navigationItem.rightBarButtonItem = shareButton;
在你的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");
}
斯威夫特
let shareBar: UIBarButtonItem = UIBarButtonItem.init(barButtonSystemItem:.Action, target: self, action: Selector("userDidTapShare"))
self.navigationItem.rightBarButtonItem = shareBar
func userDidTapShare() {
//Implementation goes here ...
}
在 Swift 3.0 中
将此代码写入
viewDidLoad
let btnShare = UIBarButtonItem(barButtonSystemItem:.action, target: self, action: #selector(btnShare_clicked))
self.navigationItem.rightBarButtonItem = btnShare
分享按钮操作
func btnShare_clicked() {
print("Share button clicked")
}
斯威夫特5
let shareButtonItem = UIBarButtonItem(
barButtonSystemItem: .action,
target: self,
action: #selector(shareButtonClick)
)
navigationItem.rightBarButtonItem = shareButtonItem