从自定义单元格调用 ActivityViewController

Calling ActivityViewController from Custom Cell

我的 TableView 应用程序有一个自定义单元格。 TableViewController 称为 "BlogView"。我的自定义单元格上有几个按钮,其中一个是共享按钮。我想在按下其中一个按钮时显示一个 UIActivityViewController。

在我的自定义单元格的 header 中,我有一个 属性:

@property (nonatomic, retain) BlogView *myViewController;

在自定义单元格中,我有 layoutSubview:

self.commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.commentButton addTarget:self action:@selector(didTapCommentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.commentButton setTitle:@"Share" forState:UIControlStateNormal];
    [self.commentButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.commentButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];

对于选择器 didTapCommentButtonAction 我有:

- (void)didTapCommentButtonAction:(id)sender
{
    NSLog(@"CommentButtonTAPPED");
    Mail *mail = [[Mail alloc]init];
    NSString *html = self.prayerObject[@"Request"];
    NSString *thetitle = [self.prayerObject[@"Title"] stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    NSString *thedate = self.prayerObject[@"dateMade"];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MMM_dd_yyyy"];
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
    NSDate *theNewDate1 = [dateFormat dateFromString:thedate];
    NSString *theNewDate = [dateFormat stringFromDate:theNewDate1];

    mail.thehtml = html;
    self.nameofhtmlfile = [[[[@"http://www.iprayed4u.net/app/" stringByAppendingString:thetitle] stringByAppendingString:@"_"] stringByAppendingString:theNewDate] stringByAppendingString:@".html"];
    //  Reminder *thereminder = [[Reminder alloc] init];
    //thereminder.thehtml = html;
    //thereminder.thetitle = thetitle;
    //thereminder.thedate = thedate;

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[self] applicationActivities:@[mail]];


    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
        activityVC.excludedActivityTypes = @[ UIActivityTypePostToWeibo,

                                              UIActivityTypeCopyToPasteboard,

                                              UIActivityTypeAssignToContact,
                                              UIActivityTypeMail,
                                              UIActivityTypePrint

                                              ];

    }
    else {

        activityVC.excludedActivityTypes = @[ UIActivityTypePostToWeibo,

                                              UIActivityTypeCopyToPasteboard,

                                              UIActivityTypeAssignToContact,
                                              UIActivityTypeMail,
                                              UIActivityTypePrint,
                                              UIActivityTypeAirDrop
                                              ];

    }

    NSLog(@"Test");
       [self.myViewController presentViewController: activityVC animated: YES completion: nil];

   }

在BlogView.m

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
                        object:(PFObject *)object
{
    static NSString *CellIdentifier = @"Cell";

    Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[Cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    self.theObject = object;

    // Configure the cell to show todo item with a priority at the bottom
    cell.profileName.text = object[@"Title"];
    cell.contentLabel.text = object[@"Request"];
    cell.firstName = object[@"FirstName"];
    cell.lastName = object[@"LastName"];
    cell.iostoken = object[@"DeviceID"];
    cell.request = object[@"Title"];
    cell.prayerObject = object;
    PFFile *thumbnail = object[@"ProfilePic"];
    cell.profilePic.image = [UIImage imageNamed:@"AppIcon60x60@2x.png"];
    [thumbnail getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {

        UIImage *thumbnailImage = [UIImage imageWithData:imageData];
        UIImageView *thumbnailImageView = [[UIImageView alloc] initWithImage:thumbnailImage];

        cell.profilePic.image = thumbnailImage;

    }];
    NSString *dates = object[@"dateMade"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MMM_dd_yyyy"];
   NSDate *datefromstring = [formatter dateFromString:dates];
    NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
    [formatter2 setDateFormat:@"MMM dd, yyyy"];
    cell.dateLabel.text = [formatter2 stringFromDate:datefromstring];
    UIFont *cellFont = [UIFont fontWithName:@"Verdana-Bold" size:15];
    UIFont *cellFont2 = [UIFont fontWithName:@"Verdana-Bold" size:12];



    return cell;
}

我没有收到任何警告或错误,但是当我点击按钮时,没有任何反应。

第一步: 从您单元格上的按钮获取一个 IBOutlet 到 CustomCell.h 文件...

@property (weak, nonatomic) IBOutlet UIButton *commentButton;

第二步:在 cellForRowAtIndexPath 中,为每个单元格上的按钮添加一个选择器,并按您想要的方式装饰它...注意 我们正在将选择器添加到每个 'cell button' 现在...不是 self.commentButton

[cell.commentButton addTarget:self action:@selector(didTapCommentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.commentButton setTitle:@"Share" forState:UIControlStateNormal];
[cell.commentButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell.commentButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];

第三步: 在 BlogView.m 文件内而不是在 CustomCell.m

内实现选择器方法