我想使用以下代码以我自己的字体自定义所有按钮:

// Custom fonts for button with tag
for (UIButton *customButton in [[self view] subviews]) {
    if (customButton.tag == 1) {
        customButton.titleLabel.font = [UIFont fontWithName:@"OpenSans-Regular" size:14];
    }
}


但是我在调​​试器控制台上收到此错误消息:

2013-09-21 00:33:33.160 Test[794:907] -[UILabel titleLabel]: unrecognized selector sent to instance 0x1dda8d80
2013-09-21 00:33:33.165 Test[794:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILabel titleLabel]: unrecognized selector sent to instance 0x1dda8d80'


我做错了什么?我正在使用XCode 4.6.3,并定位到iOS6。谢谢...

最佳答案

您发送消息titleLabel的对象不是UIButton,它已经是UILabel且标签= 1,您需要确保访问UIButton对象:

for (UIButton *customButton in [[self view] subviews]) {
    if ((customButton.tag == 1) && ([customButton isKindOfClass:[UIButton class]])) {
        customButton.titleLabel.font = [UIFont fontWithName:@"OpenSans-Regular" size:14];
    }
}

10-08 17:38