Upputton的titleLabel.text未显示在Cocoa Touch上

时间:2010-12-09 19:14:31

标签: objective-c cocoa cocoa-touch ios

在运行时创建新的UIButton并设置其titleLabel的文本后,按钮的文本仍未显示。

我做错了什么?

-(IBAction) cloneMe: (id) sender{

    if (!currentY) {
        currentY = [sender frame].origin.y;
    }

    UIButton *clone = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    CGRect cloneFrame = [sender frame];
    cloneFrame.origin.y += currentY + cloneFrame.size.height + 30;
    clone.frame = cloneFrame;
    [clone titleLabel].text = @"I'm a clone";

    [[sender superview] addSubview:clone];

    currentY = cloneFrame.origin.y + cloneFrame.size.height;


}

2 个答案:

答案 0 :(得分:12)

你需要:

[clone setTitle:@"I'm a clone" forState:UIControlStateNormal];

查看UIControl API文档,了解forState:参数的其他有效值。

答案 1 :(得分:2)

使用setTitle:forState:设置按钮的标题。