gpt4 book ai didi

ios - 显示日历动态还显示月份

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:58:53 24 4
gpt4 key购买 nike

这里我在顶部使用了一个标签来显示今天的月份和年份。我还有 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

两个按钮操作:

- (IBAction)calRight:(id)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)calLeft:(id)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)calRight:(id)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)calLeft:(id)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)updateDateLabelsGoingForward:(BOOL) goForward andFromDay:(NSDate*) 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;
}
}
}

最佳答案

全局定义一个 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)calRight:(id)sender {
firstdate = [NSDate dateWithTimeInterval:86400 sinceDate:firstdate];
[self dateChange];
}

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com