自定义单元格中按钮的快速 UIViewController

标签 swift uiviewcontroller

我有一个带有标签 View 的自定义表格 View 单元格。我添加了 tapGesture 以在单击该 View 时调用函数。 目前我的自定义 View 单元格在它自己的 swift 文件中。我添加了以下代码以在单击该标签时启用“共享扩展”。

  CustomCellView.swift

  10 myLabel.isUserInteractionEnabled = true
  11 let tap = UITapGestureRecognizer(target: self, action: #selector(tapLabelGesture))
  12 myLabel.addGestureRecognizer(tap)

  13 func tapLabelGesture() {
  14   print("Clicked on Label ")
       let url="www.google.com"
  15   let activityVC = UIActivityViewController(activityItems: [url], applicationActivities: nil)

  16   activityVC.popoverPresentationController?.sourceView = self.view
  17   self.present(activityVC, animated: true, completion: nil)
  18 }

我在 self.view 的第 16 行和 self.present() 的第 17 行收到编译器错误。问题是如何为弹出窗口提供 View ?

我将这段代码用于另一个 View (没有 TableView 或单元格)作为测试,它运行良好。所以我正在尝试对 TableView /单元格执行相同的技术。我该如何解决这个问题?感谢您的帮助。

最佳答案

对于第 16 行:

你收到一个错误提示你的类 CustomCellView 没有成员 view 因为你的类是 UITableViewCell 的子类而不是 UIViewController 因为 UIViewController 具有该属性,而您的 CustomCellView 具有用于该属性的 contentView

对于第 17 行:

与上面相同,您的类不是 UIViewController 的子类,这就是为什么您不能为此使用 self.present 的原因。

解决方案:

除了使用 UITapGestureRecognizer,您还可以使用 UIButton,您可以将其放在 UILabelUIViewController 类中在您的 cellForRowAt 方法中添加 button.tagbutton.addTarget

然后您可以在按钮方法中添加代码并显示 UIActivityViewController

希望这会有所帮助。

关于自定义单元格中按钮的快速 UIViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47842096/

相关文章:

ios - 使用自定义转换时如何处理呈现 UIViewController 中的方向更改

ios - Swift - Firebase - 订单收集

swift - 如何让 task2 在 task1 快速完成后执行?使用 dispatch_sync?

ios - 获取持久协调器时核心数据奇怪崩溃

ios - WKWebview 允许LinkPreview 错误地中断文本选择

swift - 初始 View Controller 可以工作吗?

ios - 以编程方式将数据从一个 View Controller 传递到现有 View Controller

ios - 在 UIViewController 中添加搜索显示 Controller

ios - 是什么导致 UIViewController View 的 View 大小不正确?

ios - 禁用 24 小时时间的 swift iPhone 设置会导致错误