0

我在 xcode 4.5 和 sdk 6.0 中发现了另一个恼人的错误:当我运行以下代码时:

UIColor *newcolor = [UIColor colorWithCIColor:[CIColor colorWithString:@"1 1 1 1"]];
[button setTitleColor:newcolor forState:UIControlStateNormal];
UILabel *lbl = selectedbutton.titleLabel;

它总是因错误而失败:

-[UICIColor colorSpaceName]: unrecognized selector sent to instance 0xa9864f0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICIColor colorSpaceName]: unrecognized selector sent to instance 0xa9864f0'
*** First throw call stack: [...] 
libc++abi.dylib: terminate called throwing an exception
4

2 回答 2

1

我找到了一种解决方法:在使用我的 colorWithCIColor 之前,我使用以下方法复制了它:

newcolor = [UIColor colorWithCGColor:newcolor.CGColor];

它解决了崩溃。反正很奇怪

于 2012-09-26T13:04:42.210 回答
0

我的情况和你一样,我有一串 RGB UIColor 的组成值

虽然使用 CIColor colorWithString: 更紧凑以消除我手动转换的错误:

NSArray * colorParts = [color componentsSeparatedByString: @" "];

CGFloat red = [[colorParts objectAtIndex:0] floatValue];
CGFloat green = [[colorParts objectAtIndex:1] floatValue];
CGFloat blue = [[colorParts objectAtIndex:2] floatValue];
CGFloat alpha = [[colorParts objectAtIndex:3] floatValue];

UIColor * newColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];

[button setTitleColor:newcolor forState:UIControlStateNormal];

这当然不是最优雅的方法,但如果在更新后突然成为问题,这是一个很好的解决方法。

于 2014-10-02T23:42:31.520 回答