在自己的类中显示UIActivityViewController

时间:2019-02-09 16:34:05

标签: swift uiimageview uiactivityviewcontroller

我正在开发我的应用程序,并希望共享图像。 首先,我想从我自己的名为“ PostCell” 的类(用于处理原型单元的类)中展示UIActivityViewController。 我收到消息“使用未解决的标识符“存在”” (请参见下图)。如何在课堂上正确调用vc?

第二我想分享实际图像,该图像位于我的postImageView中:UIImageView!在PostCell中。我必须为正确的图像使用IndexPath吗?

请您帮忙。 THX很多。

import UIKit

class 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) } }...

enter image description here

1 个答案:

答案 0 :(得分:0)

首先,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)
    }

}...