iphone - 为什么 UIButton 标题不显示?

标签 iphone cocoa-touch uiview uiviewcontroller uibutton

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

相关文章:

ios - Swift:如何在 UIAlertController 中添加自定义输入 View ?

ios - 在layoutSubviews中调用addSubView

ios - 如何检测 UIView View 与其他 UIView 的接近程度?

iphone - 如何制作一个圆形的UIView

ios - 将 segue 从 ViewController 推送到 TableViewController 不工作

iphone - 推送通知证书问题

iphone - 使用头搜索路径时是否需要包含.h?

iphone - 'NSString' 没有可见的@interface 声明选择器 'appendData'

iphone - 应用程序作为调试运行,但在发布时崩溃

ios - 如何仅在一个 UIViewController 中将自定义 UIView 添加到 UINavigationBar