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