Reputation: 177
I am setting a tag to a button within a cell:
/* GIVE THUMBNAIL TAG TO BE USED LATER */
cell.thumbnail.tag = indexPath.row;
Now I also assign a selector
to be used and call the current cell being clicked. Now what I am trying to do is when a button is clicked inside the same cell of a button that I need to grab. I want it to than call the other buttons title (the button that was assigned a tag based on the index) I need to grab that buttons title.
What I am doing:
-(IBAction) openindividualImpact: (id) sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
NSInteger rowOfTheCell = indexPath.row;
NSLog(@"rowofthecell %d", rowOfTheCell);
UIButton *button=(UIButton *)[self.view viewWithTag:rowOfTheCell];
NSString *btnTitle = [button currentTitle];
NSLog(@"User clicked %@", btnTitle);
}
Notice btnTitle
is trying to get the button title of button
based on the row that was clicked. This all works great but [button currentTitle]
fails because it is not a proper datatype. My question is how can I now take UIButton *button=(UIButton *)[self.view viewWithTag:rowOfTheCell];
and go grab the title of that button?
Hope this makes sense. Suggestions, thoughts?
Upvotes: 1
Views: 760
Reputation: 125027
This all works great but
[button currentTitle]
fails because it is not a proper datatype.
It sounds like the problem is that button
doesn't actually point to an instance of UIButton
.
how can I now take
UIButton *button=(UIButton *)[self.view viewWithTag:rowOfTheCell];
and go grab the title of that button?
You're probably doing this for every row, right? One of the rows will have index 0, which means that you'll set it's button's tag to 0. And the trouble with that is that the default value for a view's tag
property is 0. So for that row, you're getting the first subview that happens to have 0 as a tag.
There are at least two ways you could deal with the problem:
Add some number, like 1000, to the tag. This avoids the problem where other views happen to have the same tag as the button, but it's still a pretty roundabout way to get the cell that you're interested in.
Skip the tags altogether. Make the button's target it's cell, and add an action to the cell. The cell can then call -openindividualImpact:
passing itself as the sender.
Upvotes: 3