【发布时间】:2009-06-26 04:02:21
【问题描述】:
在 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”打印到控制台。关于我做错了什么有什么建议吗?
【问题讨论】:
-
两个想法: * 如果你让按钮变大会发生什么? * 您是否在 viewDidLoad 中做任何其他可能影响按钮的操作?
-
请参阅下面 dieerikh 的解决方案,因为它描述了上面使用的双重初始化程序中的错误以及对它的修复,即在便利初始化程序 [... 按钮之后单独分配框架type ...] 完成。 OP 自己的解决方案的其余部分是正确的,并且保持不变。
标签: iphone cocoa-touch uiview uiviewcontroller uibutton