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];
Following my experience, it will be called only once.
But IMO, it is better to use
removeTargetalways because code can be changed in the future. And someday, you may need to add multiple targets and selectors.Be a code in safe, scalable, and maintainable.