Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIButton如何存储不同的UI状态_Ios_Objective C_Uibutton - Fatal编程技术网

Ios UIButton如何存储不同的UI状态

Ios UIButton如何存储不同的UI状态,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我正在考虑将UILabel属性作为子视图添加到UIButton中,匹配当前存在的titleLabel属性的功能,并为其提供按钮的所有不同状态(UIControlStateNormal,UIControlStateHighlighted,等等) 我开始尝试在NSDictionary中存储状态,使用状态(包装在NSNumber中)作为字典的键,值为文本、文本颜色和阴影颜色。但我不确定基于各种不同的按钮状态组合,这是否能正常工作 到目前为止,我已经通过以下方法获得了一个成功: -(void)setSe

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

我开始尝试在
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
作为
ui按钮
子类的属性

为不同状态设置的不同属性似乎没有生效。如果我使用不同的位掩码手动设置它们,这很好,但是我想模拟默认的回退状态,就像
UIButton
为其属性所做的那样

因此,不必手动设置每个参数:

[button setValueText:@"Normal" forState:UIControlStateNormal];
[button setValueText:@"Normal" forState:UIControlStateHighighted];
我只想设置一次标题:

[button setValueText:@"Normal" forState:UIControlStateNormal];
所有州都是如此


提前谢谢

创建一个getter
valueTextForState:
,就像苹果对它所做的那样。此方法返回值的文档如下所示:

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

那么,让我们为您的财产也这样做:

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

我应该把
valueTextForState:
方法放在我的问题中,因为我实际上已经有了它。实际上,我还得到了一个与
ui按钮
方法相匹配的
currentValueText
。我只是不确定我是否可以像其他状态一样完成整个“退回到正常状态”-它们都有不同的退回状态还是都使用
uicontrolstatemormal
?我只是希望有一个更好的方法,而不是设置具体的回退。哦,我跳过了苹果文档的引用-直接去代码!很抱歉这完全回答了我的问题。谢谢!:)哦,虽然令人恼火,但似乎没有提到默认值!:(哦,好吧,我只想匹配另一个。还有一件事!有一个小的打字错误:
currentStateValues=self.customStates[@self.state];
应该是
currentStateValues=self.customStates[@state]
再次感谢!@Rich:你说得对,我发现你的编辑被错误地拒绝了,因为审稿人不费心思考。我已经修复了它。