UIButton
ユーザーがクリックしたときに、プログラムで作成したタイトルを変更しようとしています。だから、これはを作成するための私のコードですUIButton
:
myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, parentView.frame.size.width, parentView.frame.size.height)];
[myButton setBackgroundColor:[UIColor blackColor]];
[myButton setAlpha:0.7];
[myButton setTitle:@"Hello" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(userClicked:) forControlEvents:UIControlEventTouchUpInside];
[parentView addSubview:myButton];
そして、私のuserClicked:
方法では、次のことを行います。
-(void) userClicked:(UIButton*)button
{
NSLog(@"USER CLICKED!!!");
if ([NSThread isMainThread])
{
NSLog(@"is main thread");
}
[button setTitle:@"Bye" forState:UIControlStateHighlighted];
[button setTitle:@"Bye" forState:UIControlStateNormal];
[button setTitle:@"Bye" forState:UIControlStateSelected];
[self someLengthyComputation];
}
奇妙なことに、出力されたログ メッセージが表示されます。
USER CLICKED!!!
isMainThread
でも、ボタンのタイトルは変わらない!私は何を間違っていますか?
編集:複数の州のタイトルを設定しても機能しません。
EDIT2:Xcodeのデバッガーウィンドウでボタンの説明を印刷すると、正しいタイトルが表示されます!
Printing description of button->_titleView:
<UIButtonLabel: 0xa4c9310; frame = (95 216; 130 22); text = 'Bye'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0xa44f080>>