0

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

例如:如果用户选择 2 小时、4 分钟和 10 秒,则

按钮标签将为 02:04:10

现在我想将所有 3 件事分开,比如 2 将存储在一个字符串中,4 将存储在其他字符串中,10 将存储在其他字符串中。

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

4

2 回答 2

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];
于 2012-10-04T05:49:39.943 回答
2

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

 -(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)];  



 }

现在只需设置您的逻辑

于 2012-10-04T05:50:28.817 回答