【问题标题】:Show the UIActivityViewController in own class在自己的类中显示 UIActivityViewController
【发布时间】:2019-02-09 16:34:04
【问题描述】:

我正在开发我的应用程序并想分享一张图片。 首先我想从我自己的名为 "PostCell" 的类(处理我的原型单元的类)中展示 UIActivityViewController。 我收到消息“使用未解析的标识符‘存在’” (见下图)。如何在课堂上正确调用 vc?

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

你能帮忙吗?非常感谢。

导入 UIKit

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

【问题讨论】:

    标签: swift uiimageview uiactivityviewcontroller


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多