Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I removeTarget before I addTarget

UIControl - changing assigned selectors: addTarget & removeTarget

States that you should remove the target before changing to another. However what if I am setting the target in cellForRowAtIndexPath? Should I remove the target before adding it again even if it is not changing? Will it call the method twice if I don't remove it or will it just overwrite it?

[cell.cellSwitch removeTarget:self action:@selector(notifySwitchChanged:) forControlEvents:UIControlEventValueChanged];
[cell.cellSwitch addTarget:self action:@selector(notifySwitchChanged:) forControlEvents:UIControlEventValueChanged];
like image 824
Bot Avatar asked Apr 23 '12 17:04

Bot


1 Answers

Instead of adding/removing targets, I've found that if I'm already subclassing the UITableViewCell, I'll add a new delegate and set the delegate to the view controller. That way, any methods called on the delegate can pass in the entire cell and therefore I can get the index path of the cell by calling UITableView's indexPathForCell method.

like image 95
ninjaneer Avatar answered Oct 11 '22 09:10

ninjaneer