点击按钮(位于 UICollectionViewController 中)时如何激活共享扩展(UIActivityViewController)?
How to activate share extension (UIActivityViewController) when a button tapped (which is in UICollectionViewController)?
我正在尝试向其中一个 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 不应该处理任何业务逻辑。它应该在视图控制器中实现。
但我不知道如何解决这个问题。
您可以做的是定义一个单元格协议,您的表格视图可以遵守该协议。当点击 collectionView 上的共享按钮时,调用此委托方法,传入相关信息,例如单元格标签或该 collectionView 单元格的模型。使用该信息,您可以从 viewController.
中显示 ActivityController
你这样调用do
class Cell: UITableViewCell {
@IBOutlet let shareButton: UIButton!
}
class VC: UIViewController, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? Cell {
cell?.shareButton.addTarget(self, action: #selector(VC.shareButtonPressed(_:)), for: .touchUpInside)
}
}
func shareButtonPressed(_ button: UIButton) {
//code here
}
}
我正在尝试向其中一个 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 不应该处理任何业务逻辑。它应该在视图控制器中实现。
但我不知道如何解决这个问题。
您可以做的是定义一个单元格协议,您的表格视图可以遵守该协议。当点击 collectionView 上的共享按钮时,调用此委托方法,传入相关信息,例如单元格标签或该 collectionView 单元格的模型。使用该信息,您可以从 viewController.
中显示 ActivityController你这样调用do
class Cell: UITableViewCell {
@IBOutlet let shareButton: UIButton!
}
class VC: UIViewController, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? Cell {
cell?.shareButton.addTarget(self, action: #selector(VC.shareButtonPressed(_:)), for: .touchUpInside)
}
}
func shareButtonPressed(_ button: UIButton) {
//code here
}
}