domesticcatsoftware / DCRoundSwitch

A 'modern' replica of UISwitch.
MIT License
765 stars 162 forks source link

Crash in ARC project when adding a target after setting .on property #26

Open markbeaton opened 12 years ago

markbeaton commented 12 years ago

If you add a target immediately after setting the "on" property, there's an EXC_BAD_ACCESS triggered when the switch slide animation completes.

E.g.

toggle.on = someBoolean;
[toggle addTarget:self action:@selector(aSelector:) forControlEvents:UIControlEventValueChanged];

Perhaps something to do with the [[self allTargets] makeObjectsPerformSelector:@selector(retain)]; and [[self allTargets] makeObjectsPerformSelector:@selector(release)]; in the property setter - i.e. [self allTargets] is empty when the animation begins, but has the new target when the animation completes, causing an over-release?

jyn commented 12 years ago

I can confirm this behaviour as well.

I solved it by setting the value AFTER the addTarget call. So

[toggle addTarget:self action:@selector(aSelector:) forControlEvents:UIControlEventValueChanged];
toggle.on = someBoolean;
nickcheng commented 11 years ago

@jyn 's solution worked. Thank you!

thomasdao commented 11 years ago

thanks so much for this :)

pbodsk commented 11 years ago

@jyn 's solution worked for me as well. Thank you indeed :+1:

clementprem commented 11 years ago

@jyn thank you so much ,, you saved mi day :)

BrennaBlackwell commented 11 years ago

@jyn is my hero!