0

我的 rss 应用程序几乎完成了,但我需要在网页视图的右上角有一个“分享”按钮来查看文章。到目前为止,我已经设置了代码以在文章标题旁边显示“操作”按钮,但由于没有实现真正的代码,它什么都不做:

在此处输入图像描述

我通过基于用于实现“刷新”选项的表格视图单元格的代码来实现它,我只是更改了图标。我使用一个免费的开源项目将这个应用程序放在一起,我在弄清楚如何使该按钮显示从应用程序底部拉出的共享表时遇到了一些麻烦,如下所示:

在此处输入图像描述 (图片来自谷歌)

我试图在导航栏中直接实现一个按钮,但我不能,因为网络视图覆盖了整个屏幕:

在此处输入图像描述

因此,由于我无法将按钮直接放入控制器中,因此我必须像这样对其进行编码:

在“ -(void)viewDidLoad { ” 内的我的 RSSDetail.m 类中:

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

我在同一个类文件中也有这个:

-(void) shareButton { 

  self.webView.userInteractionEnabled = YES; 
  self.webView.aplha = 0.3;  

  } 

如您所见,该类中的这一点代码将图标放在我想要的位置。但我的问题是,有没有我可以在我的“-(void) shareButton”方法中实现的代码来实现共享功能?

另外,我需要按钮实际上说“共享”而不是图标,无论如何我可以更改“操作”rightBarButton 方法的代码以允许我输入单词而不是图标?

提前致谢

4

1 回答 1

0

As I understand you want to implement iOS native sharing functionality. Here is an example of implementation:

- (void)shareButtonPressed {
    // 1
    // If you want to use UIImage, make sure you have image with that name, otherwise it will crash. 
    NSArray *activityItems = @[@"Message to share", [UIImage imageNamed:@"imageToShare"]];

    // 2
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];

    // 3
    activityViewController.excludedActivityTypes = @[UIActivityTypeCopyToPasteboard, UIActivityTypeAirDrop, UIActivityTypeAssignToContact, UIActivityTypeAddToReadingList, UIActivityTypePrint, UIActivityTypeSaveToCameraRoll, UIActivityTypeMessage, UIActivityTypeMail];

    // 4
    [activityViewController setCompletionHandler:^(__unused NSString *activityType, __unused BOOL completed){
        // Custom completion implementation
    }];
    [self presentViewController:activityViewController animated:YES completion:NULL];
}

Description:

  1. Prepare activity items which you want to share such string, image, URL.
  2. Initialize activity view controller with the items you want to share + custom activities (only if needed). Custom activities means that you will create your own buttons using UIActivity with own actions, images, titles (more info here). For example, implement sharing to a third party app which does not have share extension.
  3. Here you can specify excluded activity types. In this example I have removed everything such as copy, airdrop and etc.
  4. In completion block you can see which activityType was selected and whether user cancelled or not.

More information on UIActivityViewController can be found here.

Regarding second part of your question about UIBarButtonItem. UIBarButtonItem can be initialised with custom title using this:

UIBarButtonItem *shareButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Share" style:UIBarButtonItemStylePlain target:self action:@selector(shareButtonPressed)];
self.navigationItem.rightBarButtonItem = shareButtonItem;

Be aware that UIBarButtonItem has UIBarButtonItemStyle which you can change. UIBarButtonItemStylePlain (will create a button with a regular font) and UIBarButtonItemStyleDone (will create a button with a bold font).

于 2015-08-24T15:34:29.920 回答