UISwitch setThumbTintColor causing crash (iOS 6 only)? UISwitch setThumbTintColor causing crash (iOS 6 only)? ios ios

UISwitch setThumbTintColor causing crash (iOS 6 only)?


I was also doing this tutorial and had the same problem. (Not sure why you don't experience this, as both my hand typed in code and the solution code have the same problem for me?)

The first segue would happen ok, but after going back the next segue would fail.

After setting a global exception breakpoint I could see thumbColorTint in the call stack when the exception was generated. I made a guess that the object was being released too early. To fix I created a property in my app delegate..(you don't need to do it in the app delegate just the object that you are setting the UISwitch appearance, which in my case was the appdelegate)

@interface SurfsUpAppDelegate()@property (strong, nonatomic) UIColor *thumbTintColor;@end

Then I set it up as so

[self setThumbTintColor:[UIColor colorWithRed:0.211 green:0.550 blue:1.000 alpha:1.000]];[[UISwitch appearance] setThumbTintColor:[self thumbTintColor]];

And now everything works as expected as the object is not released early. This is probably a defect and the object is released even though it is still needed. UISwitch seems to have a defect for the API :(


I also ran into this bug with Apple's UISwitch over-releasing. I have a similar solution, but I think its just a little bit nicer because it doesn't require the addition of a extraneous property:

UIColor *thumbTintColor =  [[UIColor alloc] initWithRed:red green:green blue:blue alpha:alpha]];//we're calling retain even though we're in ARC,// but the compiler doesn't know that[thumbTintColor performSelector:NSSelectorFromString(@"retain")]; //generates warning, but OK[[UISwitch appearance] setThumbTintColor:[self thumbTintColor]];

On the downside, it does create a compiler warning, but then - there really is a bug here, just not ours!


For now, I'm going with this per Bill's answer:

// SomeClass.m@interface SomeClass ()// ...@property (weak,   nonatomic) IBOutlet UISwitch *thumbControl;@property (strong, nonatomic)           UIColor *thumbControlThumbTintColor;// ...@end@implementation SomeClass// ...- (void)viewDidLoad{    // ...    self.thumbControl.thumbTintColor = self.thumbControlThumbTintColor = [UIColor colorWithRed:0.2 green:0.0 blue:0.0 alpha:1.0];    // ...}// ...@end