我已经创建了一个闹钟应用程序。 其中,当用户单击设置计时器按钮时,将调用选择器 View 。 选择器 View 显示小时、分钟和秒。 当用户选择它们并单击“确定”时,该计时器按钮值就会更改。
例如: 如果用户选择 2 小时 4 分 10 秒,则
按钮标签将是 02:04:10
现在我想将所有 3 个东西分开,例如 2 将存储在一个字符串中,4 将存储在另一个字符串中,10 个将存储在其他字符串中。
我应该在这里做什么? 任何人都可以帮忙吗?
最佳答案
试试这个认为可能对你有帮助....
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];
关于iphone - xcode 中的时、分、秒分开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12720929/