修复bug:date值在iPhone中可用,但不能在iPad上使用
在我的应用程序中,我必须显示一个星期的date值。它在iPhone上正常工作,但不是在iPad上。
1.此图像显示iPhone中文本框上方的date值(21 … 27)
2.此图片显示iPad中文本框上方的错误date值(3)。
对于这个我的一段代码:
- (void)viewDidLoad { [super viewDidLoad]; // here the self.startday value has been passed from one controller to another. _datelabel.text = self.startday; // here the self.startday =@"2016-02-23" [self method1]; [self getdateArray]; [self method2]; }
我的一段代码获取date:
-(void) get date { NSString *dateStr,*strday; NSString *dateFormat=@"yyyy-MM-dd"; NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; [dateFormatter1 setDateFormat:dateFormat]; NSDate *date = [dateFormatter1 dateFromString:self.startday]; NSLog(@"%@",date); NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone]; NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"]; NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date]; NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date]; NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset; NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date]; NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init]; [dateFormatters setDateFormat:dateFormat]; [dateFormatters setTimeZone:[NSTimeZone systemTimeZone]]; dateStr = [dateFormatters stringFromDate: destinationDate]; self.wkDateArray = [[NSMutableArray alloc] init]; self.wkDayArray = [[NSMutableArray alloc] init]; NSCalendar *calendar = [NSCalendar currentCalendar]; for(int i =0 ;i<7;i++) { NSDate *newdate = [destinationDate dateByAddingTimeInterval:60*60*24*i]; NSDateComponents *comp =[calendar components:NSCalendarUnitDay fromDate:newdate]; strday = [NSString stringWithFormat:@"%ld",[comp day]]; [self.wkDayArray insertObject:strday atIndex:i]; dateStr = [dateFormatters stringFromDate: newdate]; [self.wkDateArray insertObject:dateStr atIndex:i]; } NSLog(@"%@",self.wkDayArray[0]); NSLog(@"%@",self.wkDateArray[0]); }
//logging这条线:
NSDate *date = [dateFormatter1 dateFromString:self.startday]; NSLog(@"%@",date);
如果我把“date”logging器在“iPhone目的地”输出将是:
iphone:2016-02-20 18:30:00 +0000
如果我把logging器的date在“iPad目标”输出将是:
ipad:(null)
然后我得出结论,由于这个空值,一些错误的价值已经设置。
为什么它在iPad的目的地显示空值?这是什么确切的解决scheme呢?在此先感谢,请帮助我。
为iPad和iPhone创build新的通用应用程序,只更改此代码:
- (void)viewDidLoad { [super viewDidLoad]; NSString *startday = @"2016-02-23"; NSString *dateStr,*strday; NSString *dateFormat=@"yyyy-MM-dd"; NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; [dateFormatter1 setDateFormat:dateFormat]; NSDate *date = [dateFormatter1 dateFromString:startday]; NSLog(@"%@",date); }
然后告诉我,如果它在两个设备上工作。