2012-03-07 103 views
3

我遇到了一个UIButton的问题,我尝试以编程方式创建。问题是按钮出现了(即,如果我设置背景颜色,我可以清楚地看到按钮)并且是可点击的,但标题标签不可见。UIButton标题不显示

下面是我在做什么:

valueButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [valueButton addTarget:self action:@selector(openList:) forControlEvents:UIControlEventTouchUpInside]; 
    valueButton.frame = CGRectMake(160.0, decalageLabel+6.0, 120.0, 20.0); 

    [valueButton.titleLabel setTextAlignment:UITextAlignmentRight]; 
    [valueButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Neue-Light" size:17.0]]; 
    UIColor * colorLabelValue = [[UIColor alloc] initWithRed:131.0/255.0 green:159.0/255.0 blue:86.0/255.0 alpha:1.0] ; 

    [valueButton setTitleColor:colorLabelValue forState:UIControlStateNormal]; 

    if(txtValueField == NULL){ 
     txtValueField = @""; 
    } 
    valueButton.titleLabel.text = [NSString stringWithFormat:@"%@", txtValueField]; 
    NSLog(@"button : %@ ", valueButton.titleLabel.text); 
    [self.contentView addSubview:valueButton]; 

哦,我已经取得了一定的标题有一个实际值,由NSLogging其文本值,它工作正常。所以我不知道发生了什么。任何想法?

回答

9

请勿为此使用[[button titleLabel] setTitle:]。使用[button setTitle:@"blah" forControlState:UIControlStateNormal]

这使您可以设置一个不同的标题高亮,选中,等等。

+0

就是这样!非常感谢 ;) – ChrisTauziet 2012-03-07 16:26:42