如何创建标准的iOS Share按钮?

Pet*_*sey 39 cocoa-touch share uikit ios ios7

iOS人机界面指南说:

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

好的,但我如何"使用系统提供的共享按钮"?搜索文档没有任何用处.

我已经收集到我应该在响应被点击的按钮时使用UIActivityViewController,但是我如何首先创建标准的Share按钮?

Pet*_*sey 42

标准的共享按钮是一个UIBarButtonItem(因此它只能在导航栏或工具栏上).你需要创建一个"系统项目" ; 具体而言,是"行动项目"."操作"栏按钮项是"共享"按钮.


Sim*_*App 27

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

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


dan*_*ero 13

这在你的viewDidLoad中:

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

并将您的选择器方法定义为action(在我的例子中命名为"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];
}
Run Code Online (Sandbox Code Playgroud)


小智 6

Main.storyboard - > Bar Button Item - > Inspector - > System Item选择"Action"在此输入图像描述