ios - UIButton 如何存储不同的 UI 状态
<p><p>我正在考虑将 <code>UILabel</code> 属性作为 subview 添加到 <code>UIButton</code> 中,以匹配当前存在的 <code>titleLabel</code> 属性的功能,并且为它提供按钮的所有不同状态(<code>UIControlStateNormal</code>、<code>UIControlStateHighlighted</code> 等)。</p>
<p>我开始尝试将状态存储在 <code>NSDictionary</code> 中,使用状态(包装在 <code>NSNumber</code> 中)作为字典的键,值是文本,文字颜色和阴影颜色。但我不确定这是否会根据各种不同的按钮状态组合正常工作。</p>
<p>到目前为止,我有以下方法:</p>
<pre><code>-(void)setSelected:(BOOL)selected
{
super.selected = selected;
;
}
-(void)setHighlighted:(BOOL)highlighted
{
super.highlighted = highlighted;
;
}
-(void)setEnabled:(BOOL)enabled
{
super.enabled = enabled;
;
}
-(void)stateUpdated
{
;
}
-(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 = ;
self.customStates[@state] = stateValues;
}
if (text) {
stateValues[@"text"] = text;
} else {
;
}
;
}
-(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 = ;
self.customStates[@state] = stateValues;
}
if (text) {
stateValues[@"textColor"] = textColor;
} else {
;
}
;
}
-(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"];
}
</code></pre>
<p>我将 <code>valueLabel</code> 作为 <code>UIButton</code> 子类的属性。</p>
<p>为不同状态设置的不同属性似乎没有生效。如果我使用不同的位掩码手动设置它们就可以了,但我想模拟默认的回退状态,就像 <code>UIButton</code> 对其属性所做的那样。</p>
<p>所以不必手动设置每个:</p>
<pre><code>;
;
</code></pre>
<p>我希望能够只设置一次标题:</p>
<pre><code>;
</code></pre>
<p>它显示所有州。</p>
<p>提前致谢!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>创建一个 getter <code>valueTextForState:</code>,就像 Apple 对 <a href="https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/doc/uid/TP40006815-CH3-SW11" rel="noreferrer noopener nofollow"><code>titleForState:</code></a> 所做的那样.该方法返回值的文档如下:</p>
<blockquote>
<p>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.</p>
</blockquote>
<p>所以,让我们为您的属性(property)也这样做:</p>
<pre><code>- (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;
}
</code></pre>
<p>你会为你的颜色做同样的事情。那么您的 <code>updateValueLabel</code> 将如下所示:</p>
<pre><code>-(void)updateValueLabel
{
// Set the new values from the current state
self.valueLabel.text = ;
self.valueLabel.textColor = ;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - UIButton 如何存储不同的 UI 状态,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/21864588/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/21864588/
</a>
</p>
页:
[1]