【问题标题】:Separate Hours, Mins and Seconds in xcodexcode 中的时分秒分开
【发布时间】:2012-09-25 02:45:46
【问题描述】:

我创建了一个闹钟应用程序。 其中,当用户单击 Set Timer Button 时,将调用 Picker 视图。 选取器视图显示小时、分钟和秒。 当用户选择它们并单击“确定”时,该计时器按钮值会更改。

例如: 如果用户选择 2 小时 4 分 10 秒,那么

按钮标签将为 02:04:10

现在我想将所有 3 个东西分开,例如 2 将存储在一个字符串中,4 将存储在另一个字符串中,10 将存储在其他字符串中。

我应该在这里做什么? 有人可以帮忙吗?

【问题讨论】:

  • 请看nstimeformat
  • 为什么要将每个元素存储在字符串而不是整数中?

标签: iphone ios xcode ipad


【解决方案1】:

我用这种方式做了一个倒计时,可能对你有用

 -(void) viewWillAppear:(BOOL)animated
 {
 timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
 }

 -(void) updateCountdown 
 {

NSString *dateString = @"14-09-2012";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];


NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];



NSDateComponents *componentsHours = [calendar components:NSHourCalendarUnit fromDate:now];
NSDateComponents *componentMint = [calendar components:NSMinuteCalendarUnit fromDate:now];    
NSDateComponents *componentSec = [calendar components:NSSecondCalendarUnit fromDate:now];        


NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *componentsDaysDiff = [gregorianCalendar components:NSDayCalendarUnit
                                                            fromDate:now
                                                              toDate:dateFromString
                                                             options:0];



lblDaysSetting.text=[NSString stringWithFormat:@"%02d",componentsDaysDiff.day];
lblHouresSetting.text=[NSString stringWithFormat:@"%02d",(24-componentsHours.hour)];    
lblMinitSetting.text=[NSString stringWithFormat:@"%02d",(60-componentMint.minute)];  
lblSecSetting.text=[NSString stringWithFormat:@"%02d",(60-componentSec.second)];  



 }

现在只需设置您的逻辑

【讨论】:

    【解决方案2】:

    试试这个可能对你有帮助....

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"<your date format goes here"];
    NSDate *Yourcurrenttime = [dateFormatter dateFromString:string1];
    
    
    
        NSCalendar *calendar = [NSCalendar currentCalendar];
    
        NSDateComponents *components = [calendar components:(NSHourCalendarUnit |NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate: Yourcurrenttime];
    
        NSInteger hour = [components hour];
        NSInteger minutes = [components minute];
        NSInteger seconds = [components second];
    

    并将所有值存储在字符串中。

    NSString *HourSTR =[NSString stringWithFormat:@"%d",hour];
    NSString *minutesSTR =[NSString stringWithFormat:@"%d",minutes];
    NSString *secondsSTR =[NSString stringWithFormat:@"%d",seconds];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多