【问题标题】:setImage forState method sets the image for all the states on a buttonsetImage forState 方法为按钮上的所有状态设置图像
【发布时间】:2013-06-23 20:27:56
【问题描述】:

我有一个我无法弄清楚的小问题。我有大约 16 个按钮,它们作为插座集合连接到我的控制器。它们还有一个操作方法,可以使按钮的状态从默认更改为选中。我试图将所有按钮的图像更改为仅默认状态。所以我在数组中运行一个循环,并使用 setImage: forState: 方法将它们的图像设置为默认状态。但是该方法会更改所有状态(默认和选定状态)的图像。

这是我的 outlet 集合的 setter 方法

- (void) setCardsButton:(NSArray *)cardsButton
{
    _cardsButton = cardsButton;

    for (UIButton *button in cardsButton) {

         [button setImage:[UIImage imageNamed:@"card.png"] forState:UIControlStateNormal];

    }
     [self updateView];

}

【问题讨论】:

    标签: ios objective-c uibutton uiimage


    【解决方案1】:

    您为“正常”状态设置的任何属性都用于未明确设置的所有其他状态。这在UIButton setImage:forState: 的文档中有所说明。如果您想为其他州提供不同的图像,您还需要为其他州调用setImage:forState:

    【讨论】:

      【解决方案2】:

      如果您没有为其他状态指定图像,则将使用“正常”(UIControlStateNormal) 状态的图像。因此,明确设置您想要用于状态UIControlStateSelected 的图像。

      如果未设置“正常”状态,则使用系统默认值。

      【讨论】:

        猜你喜欢
        • 2013-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-21
        • 2019-03-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多