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

brvekthn  于 12个月前  发布在  iOS
关注(0)|答案(3)|浏览(104)

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

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

字符串

eyh26e7m

eyh26e7m1#

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

字符串

gcmastyq

gcmastyq2#

Swift

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

    self.navigationItem.rightBarButtonItem = shareBar

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

字符串

7hiiyaii

7hiiyaii3#

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

相关问题