如何使用Xcode在导航栏中添加分享按钮的操作

时间:2014-07-28 09:01:02

标签: ios objective-c xcode selector uinavigationitem

我正在尝试向导航栏中的分享按钮添加操作,但我不知道如何以及在何处定义我的“shareAction”方法。要添加分享按钮,我在viewWillAppear中有以下代码:

UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]
                                initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                target:self
                                action:@selector(shareAction:)];
self.navigationItem.rightBarButtonItem = shareButton;

3 个答案:

答案 0 :(得分:16)

您的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");
}

答案 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")
    }