简体   繁体   English

修复错误:日期值只能在iPhone中使用,而不能在iPad中使用

[英]Fix bug: Date values work in iPhone but not iPad

In my app,I have to display the date values for a week.It works fine on iPhone but not in iPad. 在我的应用程序中,我必须显示一周的日期值。它在iPhone上工作正常,但在iPad上工作不正常。

1.This image shows the date values(21...27) above the text box in iPhone 1.此图显示了iPhone文本框上方的日期值(21 ... 27)

在此处输入图片说明

2.This image shows the erroneous date values(3) above the text box in iPad. 2.此图显示了iPad文本框上方错误的日期值(3)。

在此处输入图片说明

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目标”中将记录器记录为“日期”,则输出将为:

iphone:2016-02-20 18:30:00 +0000

If I put logger for date in "iPad destination" output will be: 如果我将记录器的日期放到“ iPad目标”中,输出将是:

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. 为什么它在iPad目标中显示空值?这的确切解决方案是什么?谢谢,请帮帮我。

Create a new universal App for iPad and iPhone and only change this code: 为iPad和iPhone创建一个新的通用应用程序,仅更改以下代码:

- (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);

}

And then tell me if it works on both devices. 然后告诉我它是否在两种设备上均适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM