2011-10-18 4 views
2

나는 0.png에서 9.png까지의 이미지를 통해 표시하고있는 디지털 시계가 있습니다. 디지털 시계가 제대로 표시되지만 문제는 그 시간입니다. 형식은 24 시간입니다. 나는 12h 시계의 형식을 변경하려고했지만 여전히 24h에 시계를 표시합니다. 아무도 나를 12h 형식으로 시계를 표시하는 방법을 좀 도와주세요. 버튼을 사용하여 디지털 시계의 시간 형식을 변경하는 방법

는 등 내 코드

NSDate *dateShow= [NSDate dateWithTimeIntervalSinceNow:0]; 
    NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormat setTimeStyle:NSDateFormatterNoStyle]; 
    //[dateFormat setDateFormat:@"hh:mm"]; 
    /
    //NSString *dateString = [dateFormat stringFromDate:dateShow]; 


    NSDate * date = [NSDate date]; 
    NSCalendar * calendar = [[NSCalendar alloc] 
          initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateComponents * components = 
    [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date]; 

    NSInteger hour = [components hour]; 
    NSInteger minute = [components minute]; 
    NSInteger firstHourDigit = hour/10; 
    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: 
        [email protected]"0.png"; 
        break; 
        case 1: 
        [email protected]"1.png"; 
        break; 
        case 2: 
        [email protected]"2.png"; 
        break; 
        case 3: 
        [email protected]"3.png"; 
        break; 
        case 4: 
        [email protected]"4.png"; 
        break; 
        case 5: 
        [email protected]"5.png"; 
        break; 
        case 6: 
        [email protected]"6.png"; 
        break; 
        case 7: 
        [email protected]"7.png"; 
        break; 
        case 8: 
        [email protected]"8.png"; 
        break; 
        case 9: 
        [email protected]"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

일부 코드를 표시 할 수 있습니까? –

+1

일부 코드를 입력하십시오. 어떻게 시간을 결정하고 시계에 숫자를 표시합니까? 여기에는 아무도 당신을 도울 수있는 것이 없습니다. – jrturton

+0

@ DanielA.White 코드를 추가했습니다. 확인하십시오. – Rani

답변

1

사용하여 시간에 모듈 ...

value % 12 

그래서 9 % 12 = 9, 14 %, 12 = 2 ....

입니다
+0

하지만 24 시간을 클릭하면 12 시간 형식으로 12h 형식으로 표시되어야합니다 12h 형식으로 클릭하면 같은 bttton 24 시간 형식의 디지털 시계를 표시해야합니다 버튼을 가지고 있기 때문에 나는 의심의 여지가 오전. – Rani

+0

12 시간 또는 24 시간 중 원하는지를 나타내는 플래그가 필요합니다. BOOL twentyFourHour와 같은 것입니다. 그런 다음 버튼 클릭시이를 설정하십시오. 그렇다면 시간 값을 설정하는 곳은 ... hourValue = twentyFourHour? hourValue : 시간 값 % 12; –

+0

나는 내 코드에서 내가 달력 구성 요소를 사용하고 정수 변수에 추가하기 때문에 제대로 u를 얻지 못하고있다. 그 정수 변수에 대한 계산을 수행한다. 나는 어떻게 해야할지 혼란 스럽다. – Rani

관련 문제