在UIViewController的viewDidLoad方法中,我這樣做:爲什麼不顯示UIButton標題?
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行將「測試」打印到控制檯。任何建議我做錯了什麼?
兩個想法:*如果你讓你的按鈕更大? *你在做viewDidLoad中的其他任何可能影響按鈕的東西嗎? – JonathonW 2009-06-26 04:48:16
請參閱diederikh的解決方案,因爲它描述了上面使用的double初始化程序中的錯誤以及它的修復方法,即在便捷初始化程序[...按鈕與類型...]完成後單獨分配幀。 OP自己的解決方案的其餘部分是正確的,保持不變。 – eGanges 2015-06-21 03:46:40