【问题标题】:setTitle and setBackgroundImage not working for UIButton nested within UIViewsetTitle 和 setBackgroundImage 不适用于嵌套在 UIView 中的 UIButton
【发布时间】:2015-05-24 15:52:15
【问题描述】:

我有一些这样的代码:

- (void)updateUI {
    for (UIButton *cardButton in self.oneDimensionalArrayContainingCardButtons) {
        int cardButtonIndex = [self.oneDimensionalArrayContainingCardButtons indexOfObject:cardButton];

        Card *card = [self.game cardAtIndex:cardButtonIndex];
        [cardButton setTitle:[self titleForCard:card] forState:UIControlStateNormal];
        [cardButton setBackgroundImage:[self backgroundImageForCard:card] forState:UIControlStateNormal];
        cardButton.enabled = !card.isMatched;
        self.scoreLabel.text = [NSString stringWithFormat:@"Score: %d", self.game.score];
    }
}


- (NSString *)titleForCard:(Card *)card
{
    NSString *ret =  card.isChosen ? card.contents : @"";
    NSLog(ret);
    return ret;
}

- (UIImage *)backgroundImageForCard:(Card *)card   {
    return [UIImage imageNamed:card.isChosen ? @"cardback" : @"cardfront"];
}

以及如下所示的用户界面:

当我点击下面的卡片时,我会将点击的卡片设置为选中并更新 UI。通过查看日志,我可以看到内容被设置为注销为ret,这意味着我将一个非空的 NSString 传递给cardButton setTitle

问题是,背景图像没有改变,标题似乎也没有设置。

cardButton.enabled 属性似乎按预期设置。

知道会发生什么吗?

【问题讨论】:

    标签: ios objective-c uiview uibutton


    【解决方案1】:

    什么是“卡”?它是从 UIButton 扩展的自定义按钮吗?在这种情况下,问题可能出在 Card 而不是您传递给它的字符串。

    【讨论】:

    • Card 是一个模型类,扩展了 NSObject
    【解决方案2】:

    SetTitleColor 或将 backgroundColor Red/Orange 添加到 cardButton.titelLabel 以便您知道 Button 的 titleLable 是否隐藏在某个地方。如果它没有显示 titleLabel 那么你应该检查 titleLable 的框架。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      • 2011-12-20
      • 2016-12-23
      • 2013-06-28
      • 1970-01-01
      相关资源
      最近更新 更多