This is driving me nuts, I've been reading SO for hours and tried everything and cant get this button selector to work. This shouldn't be difficult.
Inside CellForItemAt i have set the button tag and try call the button.
cell.deleteCellButton.tag = indexPath.item
cell.deleteCellButton.addTarget(self, action: #selector(deleteCellButtonTapped(sender:)), for: UIControlEvents.touchUpInside)
I've tried (_:), "deleteCellButtonTapped:", and any other number of parenthesis combinations and i still get unrecognised selector. i don't know why autocomplete recommends (sender:) I've never seen this before.
then my button function:
func deleteCellButtonTapped(sender: UIButton!) {
self.packArray.remove(at: sender.tag)
print(packArray.count)
self.outerCollectionView.deleteItems(at: [IndexPath(item: sender.tag, section: 0)])
self.outerCollectionView.reloadData()
self.outerCollectionView.layoutIfNeeded()
}
Assuming you're using Swift 3, and func deleteCellButtonTapped(sender: UIButton!)
is in the same class:
addTarget(self, action: #selector(deleteCellButtonTapped(sender:)), for: .touchUpInside)
works fine.
Refering the selector method from it's class works for me.
What you can do is access selector method prefixing by it's class name .
I assume your class name is MyClassViewController
. And you code will look like:
class MyClassViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
.... // Other implementation methods
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
....// create cell object and dequeue
cell.deleteCellButton.addTarget(self, action: #selector(MyClassViewController.deleteCellButtonTapped(_:)), for: .touchUpInside)
return cell
}
func deleteCellButtonTapped(_ sender: Any) {
// your method implementation
print("Selector method called")
}
}
Hope this works fine
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.