2014-07-07 2 views
0

사용자가 버튼을 클릭하면 오늘 날짜/시간이 표시됩니다.NSDate는 nil을 계속 반환합니다.

@property (strong) NSDate *startingDate; 
@property (strong) NSDate *endingDate; 
@property (strong) NSDateFormatter *dateFormatter; 

내 초기화 방법 :

startingDate = [NSDate date]; 
    endingDate = [startingDate dateByAddingTimeInterval:600]; 

이제 내 업데이트 방법 :

-(void)updateCountdown { 

dateFormatter = [[NSDateFormatter alloc]init]; 
[dateFormatter setDateFormat:@"mm:ss"]; //format date for minutes and seconds 

NSCalendar *calendar = [NSCalendar currentCalendar]; 
NSUInteger unitFlags = NSMinuteCalendarUnit|NSSecondCalendarUnit; 
NSDateComponents *dateComponants = [calendar components:unitFlags fromDate:startingDate toDate:endingDate options:0]; 

NSInteger minutes = [dateComponants minute]; 
NSInteger seconds = [dateComponants second]; 

NSString *countdownText = [NSString stringWithFormat:@" %d Minute %d Seconds", minutes, seconds]; 
timeLabel.text = countdownText; 

NSLog (@"Startdate is %@ and Enddate is %@.", startingDate, endingDate); 
NSLog(countdownText); 

[self performSelector:@selector(updateCountdown) withObject:nil afterDelay:1]; 
} 

나는이 인스턴스 변수가

전무 시작 날짜와 종료 날짜를 반환하는 내 문제는 이제 startDate 및 endDate가 모두 nil을 반환한다는 것입니다.

편집 :

- (id)initWithCoder:(NSCoder *)aDecoder { 

self = [super initWithCoder:aDecoder]; 

if (self) { 
    startingDate = [NSDate date]; 
    endingDate = [startingDate dateByAddingTimeInterval:600]; 
} 
+2

이 코드는 어떤 클래스이며,'init' 메소드의 정확한 이름은 무엇입니까? – trojanfoe

+0

구현에 @synthesize startingDate, endDate를 추가하십시오. 또는 _startingDate 또는 self.startingDate를 사용하여 값에 액세스하십시오. –

+0

내 초기화 방법 - (ID) initWithNibName (는 NSString *) nibNameOrNil 번들 (NSBundle *) nibBundleOrNil { 자기 = 슈퍼 initWithNibName : nibNameOrNil 번들 : nibBundleOrNil]; if (self) { // 사용자 지정 초기화 startingDate = [NSDate date]; endingDate = [[NSDate date] dateByAddingTimeInterval : 600]; } return self; } – user3739958

답변

0

내 생각 엔 당신이 initWithCoder를 사용하여 초기화를 수행하지 않는 것입니다 : 내 초기화가 호출되지 않고 그대로였다 대답 모든 사람들에 대한 감사

나는 추가했다 메서드를 호출하고 initWithFrame 메서드를 호출합니다.

- (id)initWithCoder:(NSCoder *)aDecoder 

은 사용중인 것으로 xib 또는 스토리 보드를 사용할 때 호출됩니다.

방법 :

- (id)initWithFrame:(CGRect)frame 

는 프로그래밍 방식으로 뷰를 초기화 할 때 호출되는 것입니다. init 코드를 구현하는 것은 별도의 메소드이며 initWithCoder와 initWithFrame에서 호출하십시오. 이 방법을 사용하면 두 가지 호출 모두에서 동일한 동작을 얻을 수 있습니다.

+0

initWithCoder가 완벽하게 작동합니다! 감사합니다. – user3739958

+0

위대한 감사합니다! 도움이 된 것을 기쁘게 생각합니다. – gillyD

관련 문제