Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISwitch setThumbTintColor causing crash (iOS 6 only)?

UPDATE: Got a mail from Apple saying that the bug/issue has been fixed now and the next SDK release won't have this issue. Peace!

I have this in the code for my AppDelegate:

- (void) customizeAppearance {
    [[UISwitch appearance] setOnTintColor:[UIColor colorWithRed:0 green:175.0/255.0 blue:176.0/255.0 alpha:1.0]];
    [[UISwitch appearance] setTintColor:[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1.000f]];
    [[UISwitch appearance] setThumbTintColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0]];
 }

Which I then call from - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

I also use ARC. In iOS 6 my app keeps crashing. I enabled NSZombie and it keeps saying: *** -[UIDeviceRGBColor release]: message sent to deallocated instance 0x9658eb0

And now I've realized one perfectly reproducible flow for the above. When I comment out the setThumbTintColor line alone inside customizeAppearance, then everything works fine as it should. When I use the setThumbTintColor line instead, the app crashes the exact same way every time.

Is this a known issue to anyone with UISwitch/setThumbTintColor/UIColor? What else could be the cause if not the switch color?

like image 619
Bourne Avatar asked Oct 19 '12 10:10

Bourne


1 Answers

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 :(

like image 55
maninvan Avatar answered Oct 06 '22 00:10

maninvan