なぜ、タイトル表示をUIButtonないのですか?
-
20-08-2019 - |
質問
の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ライン印刷されます。私が間違ってやっている?
上の任意の提案解決
それは動作しませんなぜ私はあなたを伝えることはできませんが、私は解決策を持っています。
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];
洗面所独立ボタンの割り当てとinitからフレームを作成します。
他のヒント
問題が
でありますUIButton *b = [[UIButton buttonWithType:UIButtonTypeRoundedRect]
initWithFrame:CGRectMake(0, 0, 100, 100)];
buttonWithType
自動解放初期化されたオブジェクトを返します。オブジェクトは一度だけ初期化することができるようあなたは再びそれをinitWithFrameを送信することはできません。
別にそのフレームを設定します:
b.frame = CGRectMake(0, 0, 100, 100);
の代わりにこれを行います:
UIButton *b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b.frame = CGRectMake(0, 0, 100, 100);
[b setTitle:@"Testing" forState:UIControlStateNormal];
[b setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
[self.view addSubview:b];
事は、あなたが一緒に両方を使用しないでください、あなたは[[UIButton alloc] initWithFrame:]
や[UIButton buttonWithType:]
のために行く必要がある。
あなたは次のことを行うことができます:
UIButton *b=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
b=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[b setTitle:@"Button Title Here" forState:UIControlStateNormal];
[b setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
[self.view addSubview:b];
私は同じ問題を抱えていたし、それは私がbackgroundImageのではなく、画像を設定したことになってしまいました。私が行ったとき、それはすべてうまく働います:
[button setBackgroundImage:image forState:UIControlStateNormal];
所属していません StackOverflow