• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UIButton 如何存储不同的 UI 状态

[复制链接]
菜鸟教程小白 发表于 2022-12-12 18:57:10 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

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

-(void)setSelectedBOOL)selected
{
    super.selected = selected;

    [self stateUpdated];
}

-(void)setHighlightedBOOL)highlighted
{
    super.highlighted = highlighted;

    [self stateUpdated];
}

-(void)setEnabledBOOL)enabled
{
    super.enabled = enabled;

    [self stateUpdated];
}

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

-(void)setValueTextNSString *)text forStateUIControlState)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)setValueTextColorUIColor *)textColor forStateUIControlState)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];

它显示所有州。

提前致谢!



Best Answer-推荐答案


创建一个 getter valueTextForState:,就像 Apple 对 titleForState: 所做的那样.该方法返回值的文档如下:

The title for the specified state. If no title has been set for the specific state, this method returns the title associated with the UIControlStateNormal state.

所以,让我们为您的属性(property)也这样做:

- (NSString *)valueTextForStateUIControlState)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];
}

关于ios - UIButton 如何存储不同的 UI 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21864588/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap