【问题标题】:EXC_BAD_ACCESS on UITextAttributeFont (Invalid iOS version)UITextAttributeFont 上的 EXC_BAD_ACCESS(iOS 版本无效)
【发布时间】:2011-12-17 03:54:43
【问题描述】:

我正在使用 setTitleTextAttributes 更改 UISegmentedControl 上的字体,如下所示。

UIFont *font = [UIFont boldSystemFontOfSize:14.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:UITextAttributeFont];
[self setTitleTextAttributes:attributes forState:UIControlStateNormal];</pre></code>

这在模拟器中运行良好,但在 iPad 上运行时崩溃。错误的访问错误发生在创建字典时,我使用 NSLog 语句来验证 UITextAttributeFont 是否正在生成错误。

知道这里的问题是什么,或者是否有其他方法来设置字体?

已编辑

我刚刚意识到 iPad 运行的是 iOS 4(我相信这在 iOS >= 5 上受支持)。测试版本支持的最佳方法是什么?

根据 Dirty Henry 的建议,正确的实现如下。

if ([self respondsToSelector:@selector(setTitleTextAttributes: forState:)]) {
      UIFont *font = [UIFont boldSystemFontOfSize:14.0f];
      NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: font, UITextAttributeFont, nil];
      [self setTitleTextAttributes:attributes forState:UIControlStateNormal];
}

【问题讨论】:

  • 在进入dictionaryWithObject 方法之前检查以确保font 不为零。
  • 添加了一个 if(font != nil) 只是为了确定。没有喜悦。还有其他想法吗?
  • 不,还没有……在我看来,您似乎在做正确的事。尝试以不同的方式创建字典...例如:dictionaryWithObjects:andKeys:alloc + init(如果您不使用 ARC,请确保稍后发布)。看看崩溃是否仍然发生。

标签: iphone ios ipad uisegmentedcontrol


【解决方案1】:

您的 iPad 是否运行 iOS 5.0 或更高版本? (此版本已引入 UITextAttributeFont)。 (我也看不出你的代码有什么问题)

【讨论】:

  • 不,我自己才意识到这一点。测试支持的最佳方法是什么?
  • 您应该使用 setTitleTextAttributes:forState: 在您的 UISegmentedControl 上测试 respondsToSelector: 以查看它是否受支持。 (参见developer.apple.com/library/ios/#documentation/Cocoa/Reference/…
  • 欢迎。在您更新的问题上,您甚至可以将 boldSystemFontOfSize: 放在 if() 条件中;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-26
  • 2012-10-12
  • 2019-11-11
  • 1970-01-01
相关资源
最近更新 更多