gpt4 book ai didi

ios - 从自定义单元格中的按钮调用 UIActivityViewController

转载 作者:行者123 更新时间:2023-12-01 19:09:40 26 4
gpt4 key购买 nike

在我的表格 View 中,我有几个自定义单元格。一个称为纽扣电池,因为它包含几个按钮。其中之一是我试图调用 UIActivityViewController 的共享按钮。但是,我无法让它工作。我对 UITableViewCell 进行了子类化,并将其称为 ButtonCell。在头文件中,我链接了一个名为 Share 的 IBAction。在实现文件(ButtonCell.m)中,我尝试了以下方法。

- (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];
}

但是,编译器不喜欢自我。它没有显示 presentViewController:animated:completion 的可能性。有没有办法从带有按钮的自定义单元格中显示 UIActivityViewController。感谢你的帮助。

编辑#1
感谢 Michael 和 A-live,我已经弄清楚了。再次感谢所有的帮助。

在我的 cellForRowAtIndexPath 方法中,我使用了以下代码行...
[buttonCell.shareButton addTarget:self action:@selector(shareTapped) forControlEvents:UIControlEventTouchUpInside];  

通过添加目标,我能够检测到点击并调用 shareTapped 方法。然后这会调出 UIActivityViewController 并按我的计划工作!再次感谢所有信息和链接。

最佳答案

UITableViewCell 源自 UIView 而不是 UIViewController,这就是为什么“presentViewController:animated:completion:”不是您的子类 TableView 单元格的选项。

如果您想展示您的事件 View Controller ,您需要(以某种方式)获取对您的单元格(在表格中,在内容 View 中等)所在的 View Controller 的引用。

然后使用该 View Controller 引用(我将其命名为“myViewController”),您可以调用“[myViewController presentViewController: avc animated: YES completion: nil];

附言还有一件事:对于 Objective-C,最佳实践是变量和方法名称应该以小写字母开头(使用“share”而不是“Share”)。

关于ios - 从自定义单元格中的按钮调用 UIActivityViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17238360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com