我正在尝试向导航栏中的分享按钮添加操作,但我不知道如何以及在何处定义我的“shareAction”方法。要添加分享按钮,我在viewWillAppear中有以下代码:
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:@selector(shareAction:)];
self.navigationItem.rightBarButtonItem = shareButton;
答案 0 :(得分:16)
- (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");
}
答案 1 :(得分:6)
斯威夫特
let shareBar: UIBarButtonItem = UIBarButtonItem.init(barButtonSystemItem:.Action, target: self, action: Selector("userDidTapShare"))
self.navigationItem.rightBarButtonItem = shareBar
func userDidTapShare() {
//Implementation goes here ...
}
答案 2 :(得分:0)
在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")
}