每当 slider 移动时,我都在设置时间,我点击了此链接 Slider with real time in Label并使大部分工作正常,这是我目前正在处理的代码
//这段代码是检查系统设置是24小时制还是12小时制
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
m_is24h = (amRange.location == NSNotFound && pmRange.location == NSNotFound);
这是 slider 代码
UISlider *slider = (UISlider *)sender;
NSUInteger numberOfSlots = 24*2 - 1; //total number of 30mins slots
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"h:mm a"];
NSDate *zeroDate;
NSString *amString = [[formatter AMSymbol]uppercaseString];
NSString *timeString = [[[NSString stringWithFormat:@"12:00"]stringByAppendingString:@" "]stringByAppendingString:amString];
if(m_is24h)
{
zeroDate = [dateFormatter dateFromString:@"00:00"];
}
else
{
zeroDate = [dateFormatter dateFromString:timeString];
}
NSUInteger actualSlot = roundf(numberOfSlots*slider.value);
NSTimeInterval slotInterval = actualSlot * 30 * 60;
NSDate *slotDate = [NSDate dateWithTimeInterval:slotInterval sinceDate:zeroDate];
[dateFormatter setDateFormat:@"h:mm a"];
m_timeLabel.text = [[dateFormatter stringFromDate:slotDate]uppercaseString];
我在印度地区测试了这段代码,24 小时制和 12 小时制都工作正常。现在,当我将地区更改为日本或欧洲或任何其他地区时,移动 slider 时 24 小时制将不起作用,但是如果我将格式从设置更改为 12 小时,它会起作用,我不明白我在这里犯了什么错误。
最佳答案
我认为您将所做的事情过于复杂化了。您不需要使用基于用户使用的 12 或 24 小时格式的字符串为插槽创建日期,您可以只使用 NSDate 并让格式化程序适本地显示日期。
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (strong, nonatomic) NSDateFormatter *formatter;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.formatter = [NSDateFormatter new];
[self.formatter setLocale:[NSLocale currentLocale]];
[self.formatter setDateStyle:NSDateFormatterNoStyle];
[self.formatter setTimeStyle:NSDateFormatterShortStyle];
// Lazy way to set up the initial time
[self sliderMoved:self.slider];
}
#pragma mark - Actions
- (IBAction)sliderMoved:(UISlider *)sender {
NSUInteger slot = sender.value;
NSDate *slotDate = [self timeFromSlot:slot];
self.timeLabel.text = [self.formatter stringFromDate:slotDate];
}
#pragma mark - Private methods
/**
Converts a slot integer to a valid time in 30 minute increments
@param slot The slot number
@return An NSDate for the time representing the slot
@warning slot should be between 0 and 47
*/
- (NSDate *)timeFromSlot:(NSUInteger)slot{
if ((slot > 47)) {
return nil;
}
NSDateComponents *components = [NSDateComponents new];
[components setMinute:30 * slot];
return [[NSCalendar currentCalendar] dateFromComponents:components];
}
@end
这是完成您似乎想要的操作的完整 View Controller 实现。完整的项目是 available here如果您不想自己创建测试项目。
关于ios - 设置 UISlider 更改的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22861488/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我尝试使用不同的ssh_options在同一阶段运行capistranov.3任务。我的production.rb说:set:stage,:productionset:user,'deploy'set:ssh_options,{user:'deploy'}通过此配置,capistrano与用户deploy连接,这对于其余的任务是正确的。但是我需要将它连接到服务器中配置良好的an_other_user以完成一项特定任务。然后我的食谱说:...taskswithoriginaluser...task:my_task_with_an_other_userdoset:user,'an_othe
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
我需要检查DateTime是否采用有效的ISO8601格式。喜欢:#iso8601?我检查了ruby是否有特定方法,但没有找到。目前我正在使用date.iso8601==date来检查这个。有什么好的方法吗?编辑解释我的环境,并改变问题的范围。因此,我的项目将使用jsapiFullCalendar,这就是我需要iso8601字符串格式的原因。我想知道更好或正确的方法是什么,以正确的格式将日期保存在数据库中,或者让ActiveRecord完成它们的工作并在我需要时间信息时对其进行操作。 最佳答案 我不太明白你的问题。我假设您想检查
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build