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

Mou*_*ssa 7 xcode objective-c selector uinavigationitem ios

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

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

小智 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");
}
Run Code Online (Sandbox Code Playgroud)


Bob*_*j-C 6

迅速

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

    self.navigationItem.rightBarButtonItem = shareBar

     func userDidTapShare() {
          //Implementation goes here ...
    }
Run Code Online (Sandbox Code Playgroud)