更新:收到一封来自 Apple 的邮件,说这个错误/问题现在已经修复,下一个 SDK 版本将不会有这个问题。和平!
我的 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]];
}
然后我从 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
调用它
我也使用 ARC。在 iOS 6 中,我的应用程序不断崩溃。我启用了 NSZombie,它一直在说:*** -[UIDeviceRGBColor release]: message sent to deallocated instance 0x9658eb0
现在我已经实现了上述的一个完全可重现的流程。当我在 customizeAppearance 中单独注释掉 setThumbTintColor 行时,一切正常。当我改为使用 setThumbTintColor 行时,应用程序每次都以完全相同的方式崩溃。
对于任何使用 UISwitch/setThumbTintColor/UIColor 的人来说,这是一个已知问题吗?如果不是开关颜色,还有什么可能是原因?
最佳答案
我也在做 this教程,遇到了同样的问题。 (不知道为什么你没有遇到这种情况,因为我手写的代码和解决方案代码对我来说都是同样的问题?)
第一个 segue 会正常进行,但返回后下一个 segue 会失败。
设置全局异常断点后,我可以在生成异常时在调用堆栈中看到 thumbColorTint。我猜测该对象发布得太早了。为了解决这个问题,我在我的应用程序委托(delegate)中创建了一个属性。(你不需要在应用程序委托(delegate)中只对你正在设置 UISwitch 外观的对象执行此操作,在我的例子中是应用程序委托(delegate))
@interface SurfsUpAppDelegate()
@property (strong, nonatomic) UIColor *thumbTintColor;
@end
然后我就这样设置了
[self setThumbTintColor:[UIColor colorWithRed:0.211 green:0.550 blue:1.000 alpha:1.000]];
[[UISwitch appearance] setThumbTintColor:[self thumbTintColor]];
现在一切都按预期工作,因为对象没有提前发布。这可能是一个缺陷,即使仍然需要该对象,它也会被释放。 UISwitch 似乎有 API 缺陷:(
关于iphone - UISwitch setThumbTintColor 导致崩溃(仅限 iOS 6)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12972500/