无法在 CollectionViewCell 的按钮中使用参数添加目标 (Swift 4)
Cannot add target with parameters in CollectionViewCell's button (Swift 4)
我的 CollectionViewCell 中有一个按钮,我想在点击该按钮时触发 UIActivityViewController(通过 URL)。
我正在尝试将目标(带参数)添加到 CollectionViewCell 中的按钮,但它似乎不起作用,这是我的代码:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: verticalCellId, for: indexPath) as! VerticalCellId
if let articleURL = self.card?[indexPath.item]._uRLAddress {
cell.shareButton.addTarget(self, action: #selector(shareButtonPressed(sharedURL: articleURL)), for: .touchUpInside)
}
}
@objc func shareButtonPressed(sharedURL: String) {
let shareText = sharedURL
let activityViewController : UIActivityViewController = UIActivityViewController(activityItems: [shareText], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
}
错误是:Argument of '#selector' does not refer to an '@objc' method, property, or initializer
在没有参数传递的情况下效果很好,像这样:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: verticalCellId, for: indexPath) as! VerticalCellId
if let articleURL = self.card?[indexPath.item]._uRLAddress {
cell.shareButton.addTarget(self, action: #selector(shareButtonPressed), for: .touchUpInside)
}
}
@objc func shareButtonPressed() {
let shareText = NSLocalizedString("Share our app with friends.", comment: "share")
let activityViewController : UIActivityViewController = UIActivityViewController(activityItems: [shareText], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
}
如有任何建议,我们将不胜感激。
您不能在target/action选择器中传递任意参数。
支持的形式有
- 无参数
- 一个参数必须是动作的发送者(
shareButtonPressed(_ sender: UIButton)
)
- 一些
UIControl
项目还允许额外的 UIControlEvents
参数。
我的 CollectionViewCell 中有一个按钮,我想在点击该按钮时触发 UIActivityViewController(通过 URL)。
我正在尝试将目标(带参数)添加到 CollectionViewCell 中的按钮,但它似乎不起作用,这是我的代码:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: verticalCellId, for: indexPath) as! VerticalCellId
if let articleURL = self.card?[indexPath.item]._uRLAddress {
cell.shareButton.addTarget(self, action: #selector(shareButtonPressed(sharedURL: articleURL)), for: .touchUpInside)
}
}
@objc func shareButtonPressed(sharedURL: String) {
let shareText = sharedURL
let activityViewController : UIActivityViewController = UIActivityViewController(activityItems: [shareText], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
}
错误是:Argument of '#selector' does not refer to an '@objc' method, property, or initializer
在没有参数传递的情况下效果很好,像这样:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: verticalCellId, for: indexPath) as! VerticalCellId
if let articleURL = self.card?[indexPath.item]._uRLAddress {
cell.shareButton.addTarget(self, action: #selector(shareButtonPressed), for: .touchUpInside)
}
}
@objc func shareButtonPressed() {
let shareText = NSLocalizedString("Share our app with friends.", comment: "share")
let activityViewController : UIActivityViewController = UIActivityViewController(activityItems: [shareText], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
}
如有任何建议,我们将不胜感激。
您不能在target/action选择器中传递任意参数。
支持的形式有
- 无参数
- 一个参数必须是动作的发送者(
shareButtonPressed(_ sender: UIButton)
) - 一些
UIControl
项目还允许额外的UIControlEvents
参数。