这里我在顶部使用了一个标签来显示今年的月份。而且我还有6个标签,它会显示今天的日期,例如31dec到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];
重要的:
我需要的是我需要显示如下图像:
当前日期应显示在最后,并且应显示之前的前5个日期。
我左右两个按钮。当用户按下右键时,比如现在说它的31Dec。当用户按下右键时,它应该变为1Jan并且应该显示从1月1日开始的前5个日期。并且月份.label应该自动更改为2016年1月。如下图所示:
两个按钮动作:
- (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;
}
}
}
答案 0 :(得分:1)
首先创建一个数组,存储日期为6天...... NSArray *dates
。
然后在[NSDate date]
使用dates[0]
对其进行初始化。
使用NSDateComponents
并减去日期组件,并将前5天存储在dates[1.....4]
的数组中。
点击按钮,您可以递增或递减整个数组,并再次显示所有日期....
答案 1 :(得分:1)
全局定义一个Date对象NSDate *firstdate;
并在viewDidLoad中指定其值firstdate = [NSDate date];
现在使用以下方法显示您的日期&amp;月从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];
}
答案 2 :(得分:1)
非常简单,实际上你几乎就在那里,再添加一个变量来保存最后一个标签显示的最后一个日期,让我们不要因为通过NSDateFormatter等长路线从标签中取出日期而搞得一团糟!
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*i):(-1*i);
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;
}
}
}
希望这会有所帮助,从长远来看,这可以帮助你。