OGeek|极客世界-中国程序员成长平台

标题: ios - 显示日历动态,同时显示月份 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:00
标题: ios - 显示日历动态,同时显示月份

在这里,我在顶部使用了一个标签来显示今天的月份和年份。而且我还有 6 个标签,它们将显示今天的日期,比如 12 月 31 日到 1 月 5 日。这是代码:

这将显示从下 5 个日期开始的今天日期

 NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel];
 NSDate *today = [NSDate date];
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 NSCalendar *calendar = [NSCalendar currentCalendar];
 dateFormatter.dateFormat = @"ddMMM";
 for (NSInteger i = 0; i < 6; ++i) {
     NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:today options:nil];
     UILabel *label = (UILabel *)labelArray[i];
     label.text = [dateFormatter stringFromDate:nextDate];
 }

这将显示当前月份和年份

// show the present month
    NSDate *date = [NSDate date];

   // NSDateFormatter *date = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat=@"MMMM";
    NSString * monthString = [[dateFormatter stringFromDate:date] capitalizedString];
    dateFormatter.dateFormat=@"yyyy";
    NSString * yearString = [[dateFormatter stringFromDate:date] capitalizedString];
    dateLabel.text = [NSString stringWithFormat"%@ ,%@",monthString,yearString];

重要提示:

我需要的是如下图所示:

enter image description here

  1. 当前日期应该最后显示,并且应该显示之前的 5 个日期。

  2. 我有左右两个按钮。当用户按下右键时,比如现在说它是 12 月 31 日。当用户按右时,它应更改为 1Jan,并应显示从 1 月 1 日起的前 5 个日期。月份 .label 应自动更改为 2016 年 1 月。如下图:

enter image description here

两个按钮 Action :

- (IBAction)calRightid)sender {

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat"MMMM,yyyy"];
    NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];

    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setMonth:1];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];

    dateLabel.text = [dateFormat stringFromDate:newDate];
}

- (IBAction)calLeftid)sender {

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat"MMMM,yyyy"];
    NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];

    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setMonth:-1];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];

    dateLabel.text = [dateFormat stringFromDate:newDate];
}

我所做的这段代码只是为了改变我尝试过的月份。但不要更改我的日期标签。

请用代码解释一下。

完整编辑:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSDate *firstLabelDate = [NSDate date]; //make it class variable

    NSDate *lastLabelDate = [NSDate date]; //make it class variable

    NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel]; //make it class variable

    [self updateDateLabelsGoingForward:YES andFromDay:self.lastLabelDate];
}


- (IBAction)calRightid)sender {

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat"MMMM,yyyy"];
    NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];

    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setMonth:1];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];

    dateLabel.text = [dateFormat stringFromDate:newDate];
    [self updateDateLabelsGoingForward:YES andFromDay:self.lastLabelDate];
}

- (IBAction)calLeftid)sender {

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat"MMMM,yyyy"];
    NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];

    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setMonth:-1];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];

    dateLabel.text = [dateFormat stringFromDate:newDate];
    [self updateDateLabelsGoingForward:NO andFromDay:self.firstLabelDate];
}

- (void)updateDateLabelsGoingForwardBOOL) goForward andFromDayNSDate*) dayFrom {

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    dateFormatter.dateFormat = @"ddMMM";

    for (NSInteger i = 0; i < 6; ++i) {
        int dateToBeModified = goForward?1:-1;
        NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:dateToBeModified toDate:dayFrom options:nil];
        UILabel *label = (UILabel *)self.labelArray[i];
        label.text = [dateFormatter stringFromDate:nextDate];

        if(i==5){
            self.lastLabelDate = nextDate;
        }
        if(i==0){
            self.firstLabelDate = nextDate;
        }
    }
}



Best Answer-推荐答案


全局定义一个Date对象NSDate *firstdate;并在viewDidLoad firstdate = [NSDate date];中赋值

现在使用以下方法显示您的日期和月份 从 viewDidLoad [self dateChange];

调用此方法
-(void)dateChange
{
    NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    dateFormatter.dateFormat = @"ddMMM";
    for (NSInteger i = 0; i < 6; ++i) {
        NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:firstdate options:nil];
        UILabel *label = (UILabel *)labelArray[i];
        label.text = [dateFormatter stringFromDate:nextDate];
        if (i==5) {
            dateFormatter.dateFormat=@"MMMM";
            NSString * monthString = [[dateFormatter stringFromDate:nextDate] capitalizedString];
            dateFormatter.dateFormat=@"yyyy";
            NSString * yearString = [[dateFormatter stringFromDate:nextDate] capitalizedString];
            dateLabel.text = [NSString stringWithFormat"%@ ,%@",monthString,yearString];
        }
    }

}

现在将您的 calRight 和 calLeft 设置为波纹管

- (IBAction)calRightid)sender {
firstdate = [NSDate dateWithTimeInterval:86400 sinceDate:firstdate];
[self dateChange];
}

- (IBAction)calLeftid)sender {
    firstdate = [NSDate dateWithTimeInterval:-86400 sinceDate:firstdate];
    [self dateChange];
}

关于ios - 显示日历动态,同时显示月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34542105/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4