В моем 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)
}
Любое предложение будет оценено.





Вы не могу передаете произвольный параметр в селекторе цели / действия.
Поддерживаемые формы:
shareButtonPressed(_ sender: UIButton))UIControl допускают также дополнительный параметр UIControlEvents.
Спасибо за объяснение, @vadian. Добрый день.