2012-11-29 2 views
1

레이블과 두 개의 화살표 단추가 있습니다. 왼쪽 화살표를 밀 때 나는 그 날보다 하루를 덜 보여주고 싶다. 그리고 오른쪽 화살표를 누르면 날이 추가되어야합니다.레이블이 올바른 날짜로 새로 고침되지 않습니다.

이 순간이 내 코드입니다.

- (IBAction)addDay:(id)sender { 
    NSDateComponents *dayComponent = [[NSDateComponents alloc] init]; 
    dayComponent.day = 1; 
    NSCalendar *theCalendar = [NSCalendar currentCalendar]; 
    NSString *dateNow = _lblDate.text; 

    // Convert string to date object 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"EE. d MMMM YYYY"]; 
    NSDate *date = [dateFormat dateFromString:dateNow]; 

    date = [theCalendar dateByAddingComponents:dayComponent toDate:date options:0]; 
    NSString *dateString = [dateFormat stringFromDate:date]; 
    _lblDate.text = dateString; 

} 

- (IBAction)deleteDay:(id)sender { 

    NSDateComponents *dayComponent = [[NSDateComponents alloc] init]; 
    dayComponent.day = 1; 
    NSCalendar *theCalendar = [NSCalendar currentCalendar]; 
    NSString *dateNow = _lblDate.text; 

    // Convert string to date object 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"EE. d MMMM YYYY"]; 
    NSDate *date = [dateFormat dateFromString:dateNow]; 

    date = [theCalendar dateByAddingComponents:dayComponent toDate:date options:0]; 
    NSString *dateString = [dateFormat stringFromDate:date]; 
    _lblDate.text = dateString; 

} 

하지만 어떤 이유로 든 날짜가 잘못 되었나요? 날짜를 추가하거나 삭제할 때 패턴이 없습니다.

아무도 도와 줄 수 있습니까?

종류의 난이 주요 문제이지만, - (IBAction)deleteDay:(id)sender에, dayComponent.day = 1;dayComponent.day = -1;을해야 확실하지 않다

+0

다음을 수행 빼려면? –

+1

정확히 어떻게 일자가 틀립니까? – Hyperbole

+0

이동 모듈 방식의 사용이 코드 - (는 NSString *) shortHandDate { \t 경우 (shortHandDateFormatter == 닐) { \t \t shortHandDateFormatter = [NSDateFormatter의 ALLOC] INIT]; \t \t [shortHandDateFormatter setDateFormat : @ "EE d MMMM yyyy"]; \t} \t return [shortHandDateFormatter stringFromDate : self]; } –

답변

0

코드는 올바른 것입니다. 그러나 문자열 변환과 날짜 변환에 의존하는 것이 문제가되고 있습니다. NSDate 개체를 인스턴스 변수로 사용하고 다음 또는 이전 날짜로 진행할 때이 인스턴스 변수를 업데이트하는 것이 좋습니다. 값을 업데이트 한 후 dateformatter를 사용하여이 날짜부터 문자열을 검색 할 수 있습니다. 지금은 라벨의 텍스트를 사용하여 날짜를 생성 할 때 문제가 발생합니다. 은 그래서 당신의 addTimeInterval 필요 하루를 추가하려면 이전의 일을

enter code here 

    (IBAction)previousDay:(id)sender { 

     NSDateComponents *dayComponent = [[NSDateComponents alloc] init]; 
     dayComponent.day = -1; 
     NSCalendar *theCalendar = [NSCalendar currentCalendar]; 

     NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease]; 
     [dateFormat setDateFormat:@"EE. d MMMM YYYY"]; 

     currentDate = [[theCalendar dateByAddingComponents:dayComponent toDate:currentDate options:0] retain]; 
     NSString *dateString = [dateFormat stringFromDate:currentDate]; 
     dateLabel.text = dateString; 
    } 
0

두 가지 문제점이 있습니다.

  1. 실제로 하나 날을 감소시키는되지 않음 (또한 같다?)
  2. 는 주 스레드에서 UI를 업데이트하지 않습니다. 이러한 변경을

시도 :

- (IBAction)deleteDay:(id)sender { 

    NSDateComponents *dayComponent = [[NSDateComponents alloc] init]; 
    dayComponent.day = -1; 
    /* snip */ 
} 

그리고 ... 또한 대한

[_lblDate performSelectorOnMainThread:@selector(setText:) withObject:dateString waitUntilDone:NO];

+0

무엇이 문제 2로 인도합니까? 이 메소드는 버튼을 누를 때 호출됩니다. 그것은 메인 스레드에있을 것입니다. – rmaddy

+0

CYA 이동, 다른 스레드에서 메서드를 호출 할 수있는 경우를 대비하여 (이전에이 작업을 수행했습니다). OP는 날짜가 어떻게 잘못되었는지도 지정하지 않았으므로 내가 생각할 수있는 모든 것을 나열하고 있습니다. – Hyperbole

0

당신이 그것을

// currentDate is my instance variable. 
currentDate = [[NSDate date] retain]; 
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease]; 
[dateFormat setDateFormat:@"EE. d MMMM YYYY"]; 
dateLabel.text = [dateFormat stringFromDate:currentDate]; 

을 수행 할 수 있습니다 그리고 당신은

(IBAction)nextDay:(id)sender { 

     NSDateComponents *dayComponent = [[NSDateComponents alloc] init]; 
     dayComponent.day = 1; 
     NSCalendar *theCalendar = [NSCalendar currentCalendar]; 

     NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease]; 
     [dateFormat setDateFormat:@"EE. d MMMM YYYY"]; 

     currentDate = [[theCalendar dateByAddingComponents:dayComponent toDate:currentDate options:0] retain]; 
     NSString *dateString = [dateFormat stringFromDate:currentDate]; 
     dateLabel.text = dateString; 
    } 

같이 nextdayfunctionwill 방법은 방법 그리고 여기 다음과 같은 하루

NSDate *newDate1 = [date addTimeInterval:60*60*24]; 
당신이 형성된 검사에 대한 최신의 정확한 값을 NSLogging을 시도 않았다

은 하루

NSDate *newDate2 = [date addTimeInterval:-60*60*24];