iphone - xcode 中的时、分、秒分开

标签 iphone ios xcode ipad

我已经创建了一个闹钟应用程序。 其中,当用户单击设置计时器按钮时,将调用选择器 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/

相关文章:

jquery - Phonegap iOS6 : Proper solution to Remove form assistant bar (prev, 接下来,完成)

ios - SKPhysicsWorld 在缩放节点中模拟物理

ios - command/usr/bin/codedesign 失败,退出代码 1-代码符号错误

iphone - LibXmlParsing 如何分块解析

ios - 引用armv6验证失败,但armv6不在项目中

iphone - 为什么这个不同的 "addSubView"代码会导致行为差异

iphone - 使 UILabel 可触摸

ios - 导入CSV数据(SDK iPhone)

ios - iPhone X 初始化时显示两个不同的启动屏幕

ios - 如何从字符串中删除可选的我使用了以下代码