사용자가 버튼을 클릭하면 오늘 날짜/시간이 표시됩니다.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];
}
이 코드는 어떤 클래스이며,'init' 메소드의 정확한 이름은 무엇입니까? – trojanfoe
구현에 @synthesize startingDate, endDate를 추가하십시오. 또는 _startingDate 또는 self.startingDate를 사용하여 값에 액세스하십시오. –
내 초기화 방법 - (ID) initWithNibName (는 NSString *) nibNameOrNil 번들 (NSBundle *) nibBundleOrNil { 자기 = 슈퍼 initWithNibName : nibNameOrNil 번들 : nibBundleOrNil]; if (self) { // 사용자 지정 초기화 startingDate = [NSDate date]; endingDate = [[NSDate date] dateByAddingTimeInterval : 600]; } return self; } – user3739958