【发布时间】:2013-11-27 05:15:34
【问题描述】:
当用户单击它时,我正在尝试更改我以编程方式创建的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>>
【问题讨论】:
-
也许你的按钮不在 UIControlStateNormal 状态?
-
你在哪里添加了按钮作为子视图?
-
什么是 parentView ..?
-
@AnandK parentView 是
UIViewController的主要UIView -
但是,即使我为 UIControlStateNormal、UIControlStateSelected 和 UIControlStateHighlighted 设置了标题,标题也不会改变。我会用我的实际代码更新我的问题。
标签: ios objective-c uibutton ios7 xcode5