Douglas
Douglas

Reputation: 2524

UIActivityViewController called from button in Custom Cell

In my table view, I have several custom cells. One is called a button cell as it contains several buttons. One of them is a share button that I am trying to call a UIActivityViewController. However, I can not get it to work. I have subclassed UITableViewCell and called it ButtonCell. In the header file I have linked up an IBAction called Share. In the implementation file (ButtonCell.m), I have tried the following.

- (IBAction)Share
{

NSString *initialText = [NSString stringWithFormat:@"I like this Catherine Pooler Blog Post: "];
UIImage *image = [UIImage imageNamed:@"iTunesArtwork.png"];

UIActivityViewController *avc = [[UIActivityViewController alloc] initWithActivityItems:@[image, initialText] applicationActivities:nil];
[self presentViewController:avc animated:YES completion:nil];
}

However, the complier does not like the self. It does not show up the possibilities of presentViewController:animated:completion. Is there a way to show a UIActivityViewController from a custom cell with a button. Thanks for all your help.

EDIT #1 Thanks to Michael and A-live I have figured it out. Thanks once again for all the help.

In my cellForRowAtIndexPath method I used the following line of code...

[buttonCell.shareButton addTarget:self action:@selector(shareTapped) forControlEvents:UIControlEventTouchUpInside];  

By adding the Target I was able to detect taps and call the method shareTapped. This then bring up the UIActivityViewController and works as I had planned! Thanks again for all the information and the links.

Upvotes: 1

Views: 1568

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89509

UITableViewCell descends from UIView and not UIViewController, which is why "presentViewController:animated:completion:" is not an option for you from your subclassed table view cell.

If you want to present your Activity View Controller, you need to (somehow) get a reference to the view controller that your cell (which is in a table, which is in a content view, etc.) is sitting within.

And then with that view controller reference (which I'm naming "myViewController"), you can call "[myViewController presentViewController: avc animated: YES completion: nil];"

P.S. one more thing: for Objective-C, best practice is that variable and method names should start with lower case letters (use "share" and not "Share").

Upvotes: 3

Related Questions