在 UIViewController 的 viewDidLoad 方法中,我这样做:
UIButton *b = [[UIButton buttonWithType:UIButtonTypeRoundedRect]
initWithFrame:CGRectMake(0, 0, 100, 100)];
[b setTitle:@"Testing" forState:UIControlStateNormal];
[b setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
[self.view addSubview:b]; // EDIT: should be 'b'
NSLog(@"button title: %@", [b titleLabel].text);
按钮显示,但标题不显示。 NSLog 行将“Testing”打印到控制台。对我做错了什么有什么建议吗?
最佳答案
我无法告诉你为什么它不起作用,但我确实有一个解决方案:
UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;
b. frame = CGRectMake(0, 0, 100, 100);
[b setTitle:@"Testing" forState:UIControlStateNormal];
[b setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
[self addSubview:b];
将框架的创建与按钮的分配和初始化分开。
关于iphone - 为什么 UIButton 标题不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1047259/