【问题标题】:How do I create a standard iOS Share button?如何创建标准的 iOS 分享按钮?
【发布时间】:2013-11-13 05:24:12
【问题描述】:

The iOS Human Interface Guidelines say:

使用系统提供的分享按钮。用户熟悉此按钮的含义和行为,因此最好尽可能使用它。主要的例外情况是,如果您的应用不包含工具栏或导航栏 [,因为] 共享按钮只能在工具栏或导航栏中使用。

好的,但是我如何“使用系统提供的分享按钮”? A search of the documentation 没有任何用处。

我已经收集到 I should use UIActivityViewController in my response to the button being tapped,但我首先应该如何创建标准的“分享”按钮?

【问题讨论】:

  • 感谢您引用 HIG,b/c 链接被重定向,并且该副本丢失:(

标签: ios cocoa-touch ios7 uikit share


【解决方案1】:

标准的分享按钮是一个 UIBarButtonItem(所以它只能放在导航栏或工具栏上)。你需要create a “system item”;具体来说,an “action item”。 “操作”栏按钮项是共享按钮。

【讨论】:

    【解决方案2】:

    这里是代码。我假设你在 ViewController 里面,所以 self 有 navigationItem 属性。

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

    【讨论】:

      【解决方案3】:

      这在你的 viewDidLoad 中:

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

      并定义要作为操作调用的选择器方法(在我的例子中称为“compartir”):

      - (void) compartir:(id)sender{
      
      //Si no
      
      
      NSLog(@"shareButton pressed");
      
      
      NSString *stringtoshare= @"This is a string to share";
      UIImage *imagetoshare = img; //This is an image to share.
      
      NSArray *activityItems = @[stringtoshare, imagetoshare];
      UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
      activityVC.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo];
      [self presentViewController:activityVC animated:YES completion:nil];
      }
      

      【讨论】:

        【解决方案4】:

        Main.storyboard -> Bar Button Item -> Inspector -> System Item 选择“行动”

        【讨论】:

          【解决方案5】:

          这是 Swift 3 的代码。

          func addShareBarButtonItem() {
          
              let shareButton = UIBarButtonItem(barButtonSystemItem: .Action, target: self, action: #selector(MyViewController.shareButtonPressed))
          
              self.navigationItem.rightBarButtonItem = shareButton
          }
          
          func shareButtonPressed() {
              //Do something now!
          }
          

          【讨论】:

            【解决方案6】:

            swift 4 / Xcode 10 只有我的两分钱:

            1) 动作的初始字符已更改:

            let shareButton = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareButtonPressed))
            

            2) 添加@obj:

            @objc func shareButtonPressed() {
                //Do something now!
            }
            

            【讨论】:

              【解决方案7】:

              系统提供的操作按钮也可以使用界面生成器完全创建。为此,只需将 UIToolbar 拖放到您的视图中。比将 UIBarButtonItem 拖放到 UIToolbar 中。作为下一步,在视图层次结构中选择 UIBarButtonItem,转到 Attribute Inspector 并选择“Action”作为 System Item。 完成!

              此外,还可以将 UIBarButtonItem 与您的类连接为 IBOutlet 或 IBAction。

              请注意:我使用 Xcode 7 作为参考。

              【讨论】:

                猜你喜欢
                • 2014-06-27
                • 2016-04-28
                • 2014-10-04
                • 2021-11-23
                • 1970-01-01
                • 2016-04-07
                • 1970-01-01
                • 2013-06-02
                • 2017-12-13
                相关资源
                最近更新 更多