UIButton如何存储不同的UI状态

时间:2014-02-18 20:28:51

标签: ios objective-c uibutton

我正在考虑将UILabel属性添加到UIButton作为子视图,匹配当前存在的titleLabel属性的功能,并为其提供按钮的所有不同状态(UIControlStateNormalUIControlStateHighlighted等。)

我开始使用状态(包裹在NSDictionary中)将状态存储在NSNumber中作为字典的键,其值为文本,文本颜色和阴影颜色。但我不确定这会根据各种不同的按钮状态组合正常工作。

到目前为止,我有以下方法:

-(void)setSelected:(BOOL)selected
{
    super.selected = selected;

    [self stateUpdated];
}

-(void)setHighlighted:(BOOL)highlighted
{
    super.highlighted = highlighted;

    [self stateUpdated];
}

-(void)setEnabled:(BOOL)enabled
{
    super.enabled = enabled;

    [self stateUpdated];
}

-(void)stateUpdated
{
    [self updateValueLabel];
}

-(void)setValueText:(NSString *)text forState:(UIControlState)state
{
    NSMutableDictionary *stateValues = self.customStates[@state];

    // If we haven't set the state before create a new place to store them
    if (!stateValues) {
        stateValue = [NSMutableDictionary dictionary];
        self.customStates[@state] = stateValues;
    }

    if (text) {
        stateValues[@"text"] = text;
    } else {
        [stateValues removeObjectForKey:@"text"];
    }

    [self updateValueLabel];
}

-(void)setValueTextColor:(UIColor *)textColor forState:(UIControlState)state
{
    NSMutableDictionary *stateValues = self.customStates[@state];

    // If we haven't set the state before create a new place to store them
    if (!stateValues) {
        stateValues = [NSMutableDictionary dictionary];
        self.customStates[@state] = stateValues;
    }

    if (text) {
        stateValues[@"textColor"] = textColor;
    } else {
        [stateValues removeObjectForKey:@"textColor"];
    }

    [self updateValueLabel];
}

-(void)updateValueLabel
{
    NSMutableDictionary *currentStateValues = self.customStates[@self.state];

    // Set the new values from the current state
    self.valueLabel.text = currentStateValues[@"text"];
    self.valueLabel.textColor = currentStateValues[@"textColor"];
}

我将valueLabel作为UIButton子类的属性。

为不同状态设置的不同属性似乎没有生效。如果我使用不同的位掩码手动设置它们就可以了,但我希望模仿UIButton为其属性执行的默认回​​退状态。

所以不必手动设置每一个:

[button setValueText:@"Normal" forState:UIControlStateNormal];
[button setValueText:@"Normal" forState:UIControlStateHighighted];

我希望能够只设置一次标题:

[button setValueText:@"Normal" forState:UIControlStateNormal];

它适用于所有州。

提前致谢!

1 个答案:

答案 0 :(得分:1)

创建一个getter valueTextForState:,就像Apple与titleForState:一样。该方法的返回值的文档为:

  

指定州的标题。如果没有为特定状态设置标题,则此方法返回与UIControlStateNormal状态关联的标题。

所以,让我们为你的财产做这件事:

- (NSString *)valueTextForState:(UIControlState)state
{
    NSString *result;
    NSDictionary *currentStateValues;

    currentStateValues = self.customStates[@(state)];
    result = currentStateValues[@"text"];

    if (!result && state != UIControlStateNormal) {
        // Fall back to normal state.
        currentStateValues = self.customStates[@(UIControlStateNormal)];
        result = currentStateValues[@"text"];
    }

    return result;
}

你会为你的颜色做同样的事情。那么您的updateValueLabel将如下所示:

-(void)updateValueLabel
{
    // Set the new values from the current state
    self.valueLabel.text = [self valueTextForState:self.state];
    self.valueLabel.textColor = [self valueColorForState:self.state];
}