我正在考虑将 UILabel 属性作为 subview 添加到 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 作为 UIButton 子类的属性。
为不同状态设置的不同属性似乎没有生效。如果我使用不同的位掩码手动设置它们,那很好,但我想模拟默认回退状态,就像 UIButton 对其属性所做的那样。
因此不必手动设置每一个:
[button setValueText:@"Normal" forState:UIControlStateNormal];
[button setValueText:@"Normal" forState:UIControlStateHighighted];
我希望能够只设置一次标题:
[button setValueText:@"Normal" forState:UIControlStateNormal];
它显示所有州。
提前致谢!
最佳答案
创建一个 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 *)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];
}
关于ios - UIButton 如何存储不同的 UI 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21864588/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我正在使用i18n从头开始构建一个多语言网络应用程序,虽然我自己可以处理一大堆yml文件,但我说的语言(非常)有限,最终我想寻求外部帮助帮助。我想知道这里是否有人在使用UI插件/gem(与django上的django-rosetta不同)来处理多个翻译器,其中一些翻译器不愿意或无法处理存储库中的100多个文件,处理语言数据。谢谢&问候,安德拉斯(如果您已经在rubyonrails-talk上遇到了这个问题,我们深表歉意) 最佳答案 有一个rails3branchofthetolkgem在github上。您可以通过在Gemfi
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack