2015-01-09 67 views
1

我有我的TableView應用程序的自定義單元格。 TableViewController被稱爲「BlogView」。我的自定義單元格上有幾個按鈕,其中一個是共享按鈕。我想在按下其中一個按鈕時呈現UIActivityViewController。從自定義單元調用ActivityViewController

在我的自定義單元格的頭,我有一個屬性:

@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]; 

    } 

In 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:@"[email protected]"]; 
    [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; 
} 

我沒有收到任何警告或錯誤,但是當我點擊按鈕時,沒有任何反應。

+0

你有沒有其他的代碼?我認爲你需要在自定義單元類中使用你的按鈕屬性。那麼也許你可以添加一個選擇器到該屬性按鈕。在選擇器方法內部,您可以啓動ActivityViewController – 2015-01-09 22:23:20

+0

presentViewController位於按鈕調用的選擇器方法內,@JoshEngelsma – user717452 2015-01-09 22:25:13

+0

什麼是myViewController,什麼是avc?你是否創建或獲得了這兩個參考? – rdelmar 2015-01-09 22:25:20

回答

2

步驟一:從你的細胞在CustomCell.h文件中的按鈕獲得一個IBOutlet ...

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

第二步:在的cellForRowAtIndexPath,在選擇添加到按鈕每個單元和打扮起來,你怎麼想它...... 注意我們選擇加入到現在......不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裏面

+0

這樣做了。我對定製單元還不熟悉,並且在它們上面放置按鈕,所以這讓我感到非常緊張。我搜索了3個小時尋找類似的問題,最近我發現有人說引用父控制器,並被標記爲接受,沒有其他文本。 – user717452 2015-01-09 23:03:16

+0

超棒的男人!很高興一切正常!你明白你的錯誤是什麼嗎?基本上你認爲視圖中的按鈕連接到了你的UIButton屬性......但是它並不是沒有首先設置IBOutlet – 2015-01-09 23:04:19

+0

是的,已經長達36小時的編碼了,所以開始有點模糊。 – user717452 2015-01-09 23:05:24

相關問題