【发布时间】:2018-02-06 05:14:44
【问题描述】:
我正在尝试向 UICollectionViewCell 之一添加一些按钮或标签,但当点击共享按钮时,我似乎无法激活 UIActivityViewController。
这是我的代码:
class VerticalCollectionViewCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
containerView.addSubview(shareButton)
shareButton.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -16).isActive = true
shareButton.rightAnchor.constraint(equalTo: favoriteButton.leftAnchor, constant: -16).isActive = true
shareButton.widthAnchor.constraint(equalToConstant: 24).isActive = true
shareButton.heightAnchor.constraint(equalToConstant: 24).isActive = true
shareButton.addTarget(self, action: #selector(handleShareButton), for: .touchUpInside)
}
@objc func handleShareButton(sender: UIButton) {
let shareText = NSLocalizedString("I found something interesting you may want to take a look at.", comment: "share")
let shareImage = #imageLiteral(resourceName: "Bear")
let activityViewController : UIActivityViewController = UIActivityViewController(activityItems: [shareText, shareImage as Any], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
}
}
错误是:
Value of type 'VerticalCollectionViewCell' has no member 'view'
和
Value of type 'VerticalCollectionViewCell' has no member 'present'.
我知道 presentViewController 是一个 UIViewController 方法,UITableViewCell 没有一个叫做 presentViewController 的方法,UITableViewCell 不应该处理任何业务逻辑。它应该在视图控制器中实现。
但我不知道如何解决这个问题。
【问题讨论】:
标签: swift uicollectionview uicollectionviewcell uiactivityviewcontroller