I have a custom button in my collection view cell. I just want to pass indexPath to it but I am getting
"unrecognized selector error"
Here is my code
cell.showMapButton.addTarget(self, action: #selector(testFunc(indexPath:)), for: .touchUpInside)
and the function is
func testFunc(indexPath: IndexPath){
print("Testing indexPath \(indexPath)")
}
If I remove the indexPath argument it works fine and the function gets called but I need that argument so please help me in resolving this issue.
In the addTarget(:action:for:) method for UIButton, the action can at most accept a single UIButton or any of it's superclass as parameter. If you need the indexPath of your button, you need to make it a property of your UIButton by subclass or other means. My way of doing it is to create a subclass of UIButton that have indexPath as it's property:
class ButtonWithIndexPath: UIButton {
var indexPath:IndexPath?
}
Then add target as normal:
cell.showMapButton.addTarget(self, action: #selector(testFunc(button:)), for: .touchUpInside)
Not forgetting to set the indexPath of your button to that of which ever cell it is in
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCell", for: indexPath) as! myCell
cell.button.indexPath = indexPath
...
return cell
}
And cast it into it's custom subclass in the function to read the indexPath:
func textFunc(button: UIButton) {
let currentButton = (button as! ButtonWithIndexPath)
print(currentButton.indexPath)
}
You can pass UIButton instance with target selector parameters for button action.
try with following code:
Add/replace below code, belongs to collection view cell into your collection view data source method - cellForRowAtIndexPath
cell.showMapButton.tag = indexPath.row
cell.showMapButton.addTarget(self, action: #selector(testFunc(button:)), for: .touchUpInside)
For Swift 4 - define your selector function using #objc, like below.
#objc func testFunc(button: UIBUtton){
print("Index = \(button.tag)")
}
Related
I have a collection view and image view inside it and I added a UIButton to delete the image after selection. When I click the button it crashes and gives me this error:
AdPostViewController deleteUser]: unrecognized selector sent to instance 0x7fb588d5b7f0
Why is this happening and how do I fix this? Here is my code:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCell
let img = self.PhotoArray[indexPath.row]
cell.image.image = img
cell.deleteButton?.layer.setValue(indexPath.row, forKey: "index")
cell.deleteButton?.addTarget(self, action: Selector(("deleteUser")), for: UIControl.Event.touchUpInside)
return cell
}
func deleteUser(_ sender: UIButton) {
let i: Int = (sender.layer.value(forKey: "index")) as! Int
PhotoArray.remove(at: i)
// PhotoArray.removeAtIndex(i)
ImagesCollectionView.reloadData()
}
One problem is that you are forcing manual formation of the Objective-C selector, and you don't actually know how to form an Objective-C selector manually so you are getting it wrong. Don't do that! Let the compiler form the selector for you. That's its job. Replace
action: Selector(("deleteUser"))
with
action: #selector(deleteUser)
Also, you need to expose your deleteUser method to Objective-C explicitly:
#objc func deleteUser(_ sender: UIButton) {
Otherwise Objective-C still won't be able to introspect your class and find this method when the time comes to call it. Fortunately, when you switch to #selector syntax, the compiler will call out that issue for you!
I have a collection view where each of the cells has a delete button. I added the following code to cellForItemAt indexPath function.
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellTwo", for: indexPath) as! CustomCellTwo
cell.deleteButton.layer.setValue(indexPath.row, forKey: "index")
cell.deleteButton.addTarget(self, action: #selector(deleteCell), for: .touchUpInside)
Initially it looked as if it was working great. However, I found out that the add target function does not get called at the first tap if I scroll back and forth and then tap the delete button. If I tap again, it works as expected. Only the first tap does not work.
I have been trying to find a reason and a solution for several hours... Please help provide any ideas and advice.
Try to move buttons handling into CustomCellTwo implementation. Handle button event touchUpInside with #IBAction func. Now you can debug it with breakpoint set in this function's body.
Also add closure type variable to your CustomCellTwo to pass deleteCell calls into it. So it could also be checked with breakpoint.
Example:
class CustomCellTwo: UICollectionViewCell {
var onDelete: (() -> Void)?
#IBAction func onDeleteButtonTouch(_ sender: Any) {
onDelete?()
}
}
// in your UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellTwo", for: indexPath) as! CustomCellTwo
cell.onDelete = {
self.deleteCell(indexPath)
}
}
I have a uicollectionview with a series of custom class cells that have a few textviews and a uibutton. With over 100 cells, I just want to toggle the uibutton image for each respective cell. The uibutton is a favorites button, and like most apps I just want to favorite and "un-favorite" different cells.
NOTE: I tried to add the gesture recognizer in the class directly, but for some reason the image changes, but it highlights multiple cells instead of the specific cell that was clicked
my code:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SimpleExampleSubCell
cell.backgroundColor = UIColor.init(white: 0.10, alpha: 0.25)
cell.infoLine2TextVw.text = ""
cell.infoLine3TextVw.text = ""
if let heading_name = self.dict_dict_holder[indexPath.item]["Name"]{
cell.headerTextVw.text = heading_name
cell.infoLine1TextVw.text = self.dict_dict_holder[indexPath.item]["Phone"]
}
cell.bringSubview(toFront: cell.headerTextVw)
cell.favorite_button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(AddFavorite(withSender:))))
return cell
}
#objc func AddFavorite(withSender sender:UIButton){
print("clicked")
//The line below fails each time I run it.
sender.setImage(newImage.png,.normal)
}
Replace
#objc func addFavorite(withSender sender:UIButton){
with
// not recommended use touchUpInside
#objc func addFavorite(_ sender:UITapGestureRecognizer){
let btn = sender.view! as! UIButton
}
OR better
cell.favorite_button.addTarget(self, action:#selector(addFavorite), for: .touchUpInside)
Don't Add tapgestures to buttons , as they they have their own targets like touchUpInside or touchUpOutside and many more
table cells are reused you need to nil them inside cellForRowAt or give an else
if someCondition {
cell.favorite_button.setImage(newImage1.png,.normal)
else {
cell.favorite_button.setImage(newImage2.png,.normal)
}
you have to set the default image (plus everything you want to reset) for each cell in the prepareForReuse() method so it clears up the reused content
This question already has answers here:
UIButton causing unrecognized selector sent to instance
(5 answers)
Closed 5 years ago.
I am using a custom cell with UICollectionView, I need to define UIButton programmatically per cell.
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! ClinicListCell
cell.title.text = clinicNames[indexPath.row]
cell.subTitle.text = clinicSubs[indexPath.row]
cell.backgroundImageView.image = UIImage(named: clinicImages[indexPath.row])
cell.profileBtn.tag = indexPath.row
cell.profileBtn.addTarget(self, action: Selector(("profileBtnClicked:")), for: .touchUpInside)
return cell
}
And I have defined the following selector method in the same class.
class func profileBtnClicked(sender:UIButton) {
print("Selected")
}
I've tried by removing class/static from the selector method, but it always give me unrecognized selector sent to instance error, where am I going wrong?
Thanks.
Try this
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! ClinicListCell
cell.title.text = clinicNames[indexPath.row]
cell.subTitle.text = clinicSubs[indexPath.row]
cell.backgroundImageView.image = UIImage(named: clinicImages[indexPath.row])
cell.profileBtn.tag = indexPath.row
cell.profileBtn.addTarget(self, action: #selector(YourViewController.profileBtnClicked(sender:)), for: UIControlEvents.touchUpInside)
return cell
}
class func profileBtnClicked(sender:UIButton) {
print("Selected")
}
Is the profileBtn linked up probably? This error may happens when the name has changed or the link between the button in interface builder and the variable is removed.
Or you can try with the syntax
cell.profileBtn.addTarget(self, action: #selector("profileBtnClicked:"), for: .touchUpInside)
Try adding a #objc to your method, not strictly required in this case, and remove the class or static qualifier.
By the way, since Swift 2.2, you can create a Selector from a Swift function using the #selector operator. For instance:
let clicked = #selector(self.profileBtnClicked(sender:))
for:
#objc func profileBtnClicked(sender: UIButton) {
...
}
Technically, for NSObject based classes, the #obj qualifier should only be required for private methods.
I have 2 VCs, one of them is called HomeVC the other is DetailVC. I have a table view on HomeVC which displays cells with a label and a button. DetailVC just has a label. I am displaying an array of strings on the table view and when the button on the cell is clicked i want to carry the text in the label to the DetailVC's label.
Now i can easily do this with either didSelectRowAt method or using indexPathForSelectedRow in prepare segue method. But both cases requires me to tap on the cell itself but not the button.
I am just a beginner in swift. But to explain this there shouldn't be need for much code. So if you can, please explain with detail.
Thanks in advance.
In cellForRowAt add target to button i.e
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath)
cell.button.tag = indexPath.row
cell.button.addTarget(self, action: Selector("buttonAction:"), for: .touchUpInside)
// other cell element setup
return cell
}
And at button action get the item from array using button tag i.e
func buttonAction(sender: UIButton) {
let data = tableArray[sender.tag]
// logic to pass present detailVC
}
Hope this will work!!
If you are using collection view you can use the following
// Set The Click Action On Button
cell.bProfileImage.addTarget(self, action: #selector(connected(sender:)), for:
.touchUpInside)
cell.bProfileImage.tag = indexPath.row
Then in your function
// Function For TouchUpInside For Cell
#objc func connected(sender: UIButton) {
let data = individualChatsListArray[sender.tag]
print(data.name)
}