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

问题描述 投票:0回答:4

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

UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]
                                initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                target:self
                                action:@selector(shareAction:)];
self.navigationItem.rightBarButtonItem = shareButton;
ios objective-c xcode selector uinavigationitem
4个回答
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");
}

7
投票

斯威夫特

    let shareBar: UIBarButtonItem = UIBarButtonItem.init(barButtonSystemItem:.Action, target: self, action: Selector("userDidTapShare"))

    self.navigationItem.rightBarButtonItem = shareBar

     func userDidTapShare() {
          //Implementation goes here ...
    }

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

0
投票

斯威夫特5

let shareButtonItem = UIBarButtonItem(
    barButtonSystemItem: .action, 
    target: self, 
    action: #selector(shareButtonClick)
)
navigationItem.rightBarButtonItem = shareButtonItem
© www.soinside.com 2019 - 2024. All rights reserved.