18

更新:收到来自 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

现在我已经实现了上述的一个完全可重现的流程。当我在customAppearance 中单独注释掉setThumbTintColor 行时,一切正常。当我改用 setThumbTintColor 线时,应用程序每次都以完全相同的方式崩溃。

对于任何使用 UISwitch/setThumbTintColor/UIColor 的人来说,这是一个已知问题吗?如果不是开关颜色,还有什么原因?

4

3 回答 3

19

我也在做这个教程并且遇到了同样的问题。(不知道为什么你没有遇到这种情况,因为我的手输入代码和解决方案代码对我来说都有同样的问题?)

第一个 segue 会发生,但在返回后下一个 segue 会失败。

设置全局异常断点后,我可以在生成异常时在调用堆栈中看到 thumbColorTint。我猜测该对象被释放得太早了。为了修复我在我的应用程序委托中创建了一个属性..(您不需要在应用程序委托中执行它,只需设置您正在设置 UISwitch 外观的对象,在我的情况下是 appdelegate)

@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 似乎有缺陷 :(

于 2012-10-22T18:48:46.607 回答
3

Apple 的 UISwitch 过度释放时,我也遇到了这个错误。我有一个类似的解决方案,但我认为它更好一点,因为它不需要添加无关的属性:

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]];

不利的一面是,它确实会产生一个编译器警告,但是 - 这里确实有一个错误,只是不是我们的!

于 2013-04-19T01:53:28.683 回答
1

现在,根据比尔的回答,我将采用此方法:

// 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
于 2013-06-18T04:29:40.597 回答