【问题标题】:How to add an action to share button in navigation bar with Xcode如何使用 Xcode 在导航栏中添加操作以共享按钮
【发布时间】:2014-07-28 09:01:01
【问题描述】:

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

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

【问题讨论】:

  • Action 只是一个私有方法。您可以在当前类中定义。

标签: ios objective-c xcode selector uinavigationitem


【解决方案1】:

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

【讨论】:

  • 请详细说明你的答案,也许解释一下代码在做什么以及它是如何解决问题的。这将有助于将来看到您的代码的其他人
【解决方案2】:

斯威夫特

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

    self.navigationItem.rightBarButtonItem = shareBar

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      相关资源
      最近更新 更多