我创建了一个数字时钟,图像从0.png到9.png。此时钟工作正常。时钟显示的默认时间格式为24格式。我希望时钟以12小时的格式显示时间。我已经完成了以下代码,但问题是它总是以24小时格式显示时间:
- (void)viewDidLoad {
//timer to recursively call the showClock method.
twentyfourhour = YES;
timer=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showClock) userInfo:nil repeats:YES];
[super viewDidLoad];
}
-(void)showClock
{
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setTimeStyle:NSDateFormatterNoStyle];
NSDate * date = [NSDate date];
NSCalendar * calendar = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents * components =
[calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date];
if (twentyfourhour == YES) {
twentyfourhour = [components hour]%12;
int hour = twentyfourhour ? [components hour] : [components hour] % 12;
NSLog(@"%d",hour);
NSInteger minute = [components minute];
NSInteger firstHourDigit = hour/12;
NSInteger secondHourDigit = hour%10;
NSInteger firstMinuteDigit = minute/10;
NSInteger secondMinuteDigit = minute%10;
int res=0;
for(int i=0;i<4;i++)
{
if(i==0)
res = firstHourDigit;
else if(i==1)
res = secondHourDigit;
else if (i==2)
res = firstMinuteDigit;
else
res = secondMinuteDigit;
switch (res) {
case 0:
str_imageName=@"0.png";
break;
case 1:
str_imageName=@"1.png";
break;
case 2:
str_imageName=@"2.png";
break;
case 3:
str_imageName=@"3.png";
break;
case 4:
str_imageName=@"4.png";
break;
case 5:
str_imageName=@"5.png";
break;
case 6:
str_imageName=@"6.png";
break;
case 7:
str_imageName=@"7.png";
break;
case 8:
str_imageName=@"8.png";
break;
case 9:
str_imageName=@"9.png";
break;
default:
break;
}
if(i==0)
[minfirstImage setImage:[UIImage imageNamed:str_imageName]];
else if(i==1)
[minsecondImage setImage:[UIImage imageNamed:str_imageName]];
else if (i==2)
[secfirstImage setImage:[UIImage imageNamed:str_imageName]];
else
[secSecondImage setImage:[UIImage imageNamed:str_imageName]];
}
}
else
{
int hour1 = [components hour];
NSInteger minute = [components minute];
NSInteger firstHourDigit = hour1/10;
NSInteger secondHourDigit = hour1%10;
NSInteger firstMinuteDigit = minute/10;
NSInteger secondMinuteDigit = minute%10;
int res=0;
for(int i=0;i<4;i++)
{
if(i==0)
res = firstHourDigit;
else if(i==1)
res = secondHourDigit;
else if (i==2)
res = firstMinuteDigit;
else
res = secondMinuteDigit;
switch (res) {
case 0:
str_imageName=@"0.png";
break;
case 1:
str_imageName=@"1.png";
break;
case 2:
str_imageName=@"2.png";
break;
case 3:
str_imageName=@"3.png";
break;
case 4:
str_imageName=@"4.png";
break;
case 5:
str_imageName=@"5.png";
break;
case 6:
str_imageName=@"6.png";
break;
case 7:
str_imageName=@"7.png";
break;
case 8:
str_imageName=@"8.png";
break;
case 9:
str_imageName=@"9.png";
break;
default:
break;
}
if(i==0)
[minfirstImage setImage:[UIImage imageNamed:str_imageName]];
else if(i==1)
[minsecondImage setImage:[UIImage imageNamed:str_imageName]];
else if (i==2)
[secfirstImage setImage:[UIImage imageNamed:str_imageName]];
else
[secSecondImage setImage:[UIImage imageNamed:str_imageName]];
}
}
}
答案 0 :(得分:0)
- (void)viewDidLoad {
//timer to recursively call the showClock method.
twentyfourhour = YES;
timer=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showClock) userInfo:nil repeats:YES];
[super viewDidLoad];
}
它在顶部说二十四小时是否与它有关?也许改变它,看它是否有所作为。
答案 1 :(得分:0)
我可以看到一个可能影响你逻辑的地方。
twentyfourhour = [components hour]%12;
每当“小时”不是12的倍数时,你将获得一个非零值,因此9小时将导致9小时和15小时将导致3.因为它们不是零,所以你的twerntyfourhour将被视为是那些作业。
我希望这可以帮助你进一步解决问题。