【发布时间】:2016-01-23 16:03:22
【问题描述】:
我继承了UIButton 并具有以下prepareForInterfaceBuilder 函数:
- (void)prepareForInterfaceBuilder {
NSString *string = [self titleForState:UIControlStateNormal];
NSAttributedString *attrString = [[NSAttributedString alloc]
initWithString:string
attributes:@{
NSKernAttributeName : @4,
NSForegroundColorAttributeName : [UIColor whiteColor]
}];
[self setAttributedTitle:attrString forState:UIControlStateNormal];
}
在 Interface Builder 中查看时崩溃,因为字符串变量返回 nil,即使我在 Interface Builder 中将“测试”设置为标题。我也尝试通过调用self.titleLabel.text 来检索文本,但这也会导致nil。
如何在此函数中正确检索 titleLabel 的文本? 目前使用 Xcode 7.1 (7B91b)。
【问题讨论】:
-
能否分享一下崩溃日志?
-
我试着玩
prepareForInterfaceBuilder,看起来它不允许对按钮标题做任何事情。但是设置标题颜色\其他东西工作正常 -
@jamil65able:如何从界面生成器获取崩溃日志?请记住,这不是在标准运行时完成的。查看我的故事板时,我在问题导航器中收到代理崩溃消息。
-
@VitaliyGozhenko:奇怪的是,您可以从
UILabel检索文本,但不能从UIButton检索文本 -
可能是因为
titleLabel在运行时初始化,而不是在IB中
标签: ios xcode uibutton interface-builder