2010-12-09 42 views
0

在運行時創建一個新的UIButton並設置其titleLabel的文本後,該按鈕的文本仍然不顯示。UIButton的titleLabel.text在Cocoa Touch上不顯示

我在做什麼錯?

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


} 
+0

克隆按鈕本身是否顯示出來? – Ryan 2010-12-09 19:19:05

回答

12

您需要:

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

檢查出了forState:其他參數的有效值UIControl API文檔。

2

使用setTitle:forState:設置按鈕的標題。