In my app,I have to display the date values for a week.It works fine on iPhone but not in iPad.
1.This image shows the date values(21...27) above the text box in iPhone
2.This image shows the erroneous date values(3) above the text box in iPad.
For this My piece of code:
- (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];
}
My piece of code for method get 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]);
}
// Logger for this line:
NSDate *date = [dateFormatter1 dateFromString:self.startday];
NSLog(@"%@",date);
If I put logger for "date" in "iPhone destination" output will be:
iphone:2016-02-20 18:30:00 +0000
If I put logger for date in "iPad destination" output will be:
ipad:(null)
Then I conclude that because of this null value,some erroneous value has been sets.
Why it shows null value in iPad destination?what's the exact solution for this?Thanks in advance.Please help me out.
Create a new universal App for iPad and iPhone and only change this code:
And then tell me if it works on both devices.