Strange crash when I try to access to uibutton's titleLabel property (xcode 4.5 and IOS sdk 6.0)

孤者浪人 提交于 2019-12-25 15:25:00

问题


I found another annoying bug with xcode 4.5 and sdk 6.0 : when I run the following code :

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

It always fail with the error :

-[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

回答1:


I found a workaround : before using my colorWithCIColor, I made a copy of it with :

newcolor = [UIColor colorWithCGColor:newcolor.CGColor];

and it solves the crash. Strange, anyway




回答2:


I was in the same situation as you where I had a string of the constituent values of the RGB UIColor

Although using CIColor colorWithString: is much more compact to get rid of the error I converted it manually:

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

This certainly isn't the most elegant way of doing it but is a good fix if this suddenly becomes an issue after an update.



来源:https://stackoverflow.com/questions/12586834/strange-crash-when-i-try-to-access-to-uibuttons-titlelabel-property-xcode-4-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!