我正在开发我的应用程序并想要分享图像。 首先我想展示我自己的类“PostCell”(处理我的原型(prototype)单元的类)中的UIActivityViewController。 我收到消息“使用未解析的标识符‘存在’” (见下图)。如何在我的类(class)中正确调用 vc?
第二我想分享我的postImageView中的实际图像:UIImageView!在 PostCell 中。我必须使用 IndexPath 来获取正确的图像吗?
你能帮忙吗?非常感谢。
import UIKitclass PostCell: UITableViewCell {
@IBOutlet weak var postCaptionLabel: UILabel! @IBOutlet weak var postImageView: UIImageView! @IBOutlet weak var stuffButton: UIButton! @IBAction func shareButton(_ sender: UIButton) { if let image = UIImage(self.postImageView.image) { let vc = UIActivityViewController(activityItems: [image], applicationActivities: []) present(vc, animated: true) } }...
最佳答案
首先,UITableViewCell
没有 present
方法。这必须从 ViewController
完成,或者在单元格中创建对 ViewController
的引用,然后调用 Present。其次, self.postImageView.image 已经为您提供了一个 UIImage ,无需构造它。这是一个例子。
import UIKit
class PostCell: UITableViewCell {
@IBOutlet weak var postCaptionLabel: UILabel!
@IBOutlet weak var postImageView: UIImageView!
@IBOutlet weak var stuffButton: UIButton!
// Here you add a reference to the viewController. Remember to write cell.viewController = self in the cellForRow method
var viewController : UIViewController!
@IBAction func shareButton(_ sender: UIButton) {
// Here you remove the UIImage constructor.
if let image = self.postImageView.image {
let vc = UIActivityViewController(activityItems: [image], applicationActivities: [])
viewController.present(vc, animated: true)
}
}...
关于swift - 在自己的类中显示 UIActivityViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54608238/