Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 单击“下一步”和“上一步”按钮时更改UILabel中的日期_Ios_Objective C_Uilabel_Nsdate_Updates - Fatal编程技术网

Ios 单击“下一步”和“上一步”按钮时更改UILabel中的日期

Ios 单击“下一步”和“上一步”按钮时更改UILabel中的日期,ios,objective-c,uilabel,nsdate,updates,Ios,Objective C,Uilabel,Nsdate,Updates,在UILabel中设置日期。 单击“下一步”和“上一步”按钮应更改此标签内的日期 我正在尝试此代码,但下一个按钮单击并显示日期下一天日期,例如2015年5月1日和上一个按钮单击并设置日期2015年5月29日,下一个按钮单击不显示2015年5月2日 - (IBAction)changeToNextDay:(id)sender { NSDateComponents* deltaComps = [[NSDateComponents alloc] init]; [deltaComps s

在UILabel中设置日期。 单击“下一步”和“上一步”按钮应更改此标签内的日期

我正在尝试此代码,但下一个按钮单击并显示日期下一天日期,例如2015年5月1日和上一个按钮单击并设置日期2015年5月29日,下一个按钮单击不显示2015年5月2日

- (IBAction)changeToNextDay:(id)sender
{
    NSDateComponents* deltaComps = [[NSDateComponents alloc] init];
    [deltaComps setDay:+1];
    NSDate* tomorrow = [[NSCalendar currentCalendar]  dateByAddingComponents:deltaComps toDate:[NSDate date] options:0];

    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
    [myDateFormatter setDateFormat:@"dd-MM-yyyy"];

    NSString *stringFromDate = [myDateFormatter  stringFromDate:tomorrow];
    dateLabel.text = stringFromDate;
}

- (IBAction)changeToPreviousDay:(id)sender
{
    NSDate *datePlusOneDay = [[NSDate date] dateByAddingTimeInterval:-(60 * 60 * 24)];
    NSLog(@"datePlusOneDay=%@",datePlusOneDay);

    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
    [myDateFormatter setDateFormat:@"dd-MM-yyyy"];

    NSString *stringFromDate = [myDateFormatter stringFromDate:datePlusOneDay];
    dateLabel.text = stringFromDate;
}

前一天的功能

-(void)previousDay
{
  NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    dateString = [dateFormat stringFromDate:today];


    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [NSDateComponents new];
    comps.day =-1;
    NSDate *previousDay = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    resultDate = [dateFormat stringFromDate:previousDay];

}
第二天的活动

-(void)nextDay
{
  NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    dateString = [dateFormat stringFromDate:today];


    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [NSDateComponents new];
    comps.day =1;
    NSDate *nextDay = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    resultDate = [dateFormat stringFromDate:nextDay];;

}

您可以在前一天使用此功能

-(void)previousDay
{
  NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    dateString = [dateFormat stringFromDate:today];


    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [NSDateComponents new];
    comps.day =-1;
    NSDate *previousDay = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    resultDate = [dateFormat stringFromDate:previousDay];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self Update_Date_By:0];
}

- (IBAction)changeToNextDay:(id)sender {
    [self Update_Date_By:1];
}

- (IBAction)changeToPreviousDay:(id)sender {
    [self Update_Date_By:-1];
}

-(void)Update_Date_By:(NSInteger)value {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    NSDate *date = [dateFormat dateFromString:dateLabel.text];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [NSDateComponents new];
    dateComponents.day = value;
    if (value == 0) {
        date = [NSDate date];
    }
    NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:date options:0];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    NSString *finalDate_String = [dateFormat stringFromDate:newDate];
    dateLabel.text = finalDate_String;
}
第二天的活动

-(void)nextDay
{
  NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    dateString = [dateFormat stringFromDate:today];


    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [NSDateComponents new];
    comps.day =1;
    NSDate *nextDay = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    resultDate = [dateFormat stringFromDate:nextDay];;

}

您可以使用此功能

您可以通过简单的方式实现此功能。请按照以下步骤操作-

- (void)viewDidLoad {
    [super viewDidLoad];
    [self Update_Date_By:0];
}

- (IBAction)changeToNextDay:(id)sender {
    [self Update_Date_By:1];
}

- (IBAction)changeToPreviousDay:(id)sender {
    [self Update_Date_By:-1];
}

-(void)Update_Date_By:(NSInteger)value {
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    NSDate *date = [dateFormat dateFromString:dateLabel.text];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [NSDateComponents new];
    dateComponents.day = value;
    if (value == 0) {
        date = [NSDate date];
    }
    NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:date options:0];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    NSString *finalDate_String = [dateFormat stringFromDate:newDate];
    dateLabel.text = finalDate_String;
}
声明两个全局变量,用于格式化和递增或递减日期。例如-

导入ViewController.h

@界面视图控制器 {

}

在viewDidLoad中,定义日期格式,在UILabel中设置当前日期,并将day变量初始化为0-

day=0;
DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateFormat:@"dd/MM/yyyy"];
dateLabel.text=[DateFormatter stringFromDate:[NSDate date]];
要转到第二天,请在“changeToNextDay”按钮的主体中编写代码-

NSDate *now = [NSDate date];
day+=1;
NSDateComponents *components =[[NSDateComponents alloc] init];
[components setDay:day];
 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];  
 NSDate *newDate = [gregorian dateByAddingComponents:components toDate:now options:0];
 dateLabel.text=[DateFormatter stringFromDate:newDate];
要转到前一天,只需从“changeToNextDay”复制代码并将其粘贴到“changeToNextDay”按钮中,然后更改行-

 day-=1 from day+=1;

让我知道它是否适合您。谢谢。

您可以用一种简单的方法实现它。请按照以下步骤操作-

声明两个全局变量,用于格式化和递增或递减日期。例如-

导入ViewController.h

@界面视图控制器 {

}

在viewDidLoad中,定义日期格式,在UILabel中设置当前日期,并将day变量初始化为0-

day=0;
DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateFormat:@"dd/MM/yyyy"];
dateLabel.text=[DateFormatter stringFromDate:[NSDate date]];
要转到第二天,请在“changeToNextDay”按钮的主体中编写代码-

NSDate *now = [NSDate date];
day+=1;
NSDateComponents *components =[[NSDateComponents alloc] init];
[components setDay:day];
 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];  
 NSDate *newDate = [gregorian dateByAddingComponents:components toDate:now options:0];
 dateLabel.text=[DateFormatter stringFromDate:newDate];
要转到前一天,只需从“changeToNextDay”复制代码并将其粘贴到“changeToNextDay”按钮中,然后更改行-

 day-=1 from day+=1;
如果对你有效,请告诉我。谢谢