【问题标题】:Strange crash when I try to access to uibutton's titleLabel property (xcode 4.5 and IOS sdk 6.0)当我尝试访问 uibutton 的 titleLabel 属性时出现奇怪的崩溃(xcode 4.5 和 IOS sdk 6.0)
【发布时间】:2012-09-25 16:05:05
【问题描述】:

我在 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

【问题讨论】:

  • 当这种情况发生时查看堆栈上的符号会很有趣,以便找出导致该方法被调用的原因。如果您在字符串中明确使用浮点值是否有效:@"1.0 1.0 1.0 1.0"(或 @"1.0 1.0 1.0" 具有默认 alpha)?
  • 不,也不起作用。同样的错误。我的真实值是浮点数,我在示例中放入整数来说明。我认为这与 de CIColor 颜色空间配置文件有关。
  • 肯定会发生一些奇怪的事情,因为将用于创建颜色的代码粘贴到测试项目中并链接到 Core Image 框架不会给我任何错误。

标签: ios6 xcode4.5 unrecognized-selector


【解决方案1】:

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

newcolor = [UIColor colorWithCGColor:newcolor.CGColor];

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

【讨论】:

    【解决方案2】:

    我的情况和你一样,我有一个 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];
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 2019-04-17
      • 1970-01-01
      相关资源
      最近更新 更多