修复错误:日期值只能在iPhone上使用,而不能在iPad上使用 [英] Fix bug: Date values work in iPhone but not iPad

查看:86
本文介绍了修复错误:日期值只能在iPhone上使用,而不能在iPad上使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我必须显示一个星期的日期值.它在iPhone上可以正常工作,但在iPad上不能正常工作.

In my app,I have to display the date values for a week.It works fine on iPhone but not in iPad.

1.此图像显示了iPhone文本框上方的日期值(21 ... 27)

1.This image shows the date values(21...27) above the text box in iPhone

2.此图显示了iPad文本框上方错误的日期值(3).

2.This image shows the erroneous date values(3) above the text box in iPad.

对于我的这段代码:

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

//此行的记录器:

NSDate *date = [dateFormatter1 dateFromString:self.startday];
NSLog(@"%@",date);

如果我将日期"记录器放在"iPhone目标"中,则输出将是:

If I put logger for "date" in "iPhone destination" output will be:

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

如果我将记录器的日期放在"iPad目标"中,输出将是:

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.

为什么它在iPad目标中显示空值?这的确切解决方案是什么?谢谢,请帮帮我.

Why it shows null value in iPad destination?what's the exact solution for this?Thanks in advance.Please help me out.

推荐答案

为iPad和iPhone创建新的通用应用程序,仅更改以下代码:

Create a new universal App for iPad and iPhone and only change this code:

- (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.

这篇关于修复错误:日期值只能在iPhone上使用,而不能在iPad上使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆