如何实现上个月/下个月的按钮并显示当月的日期? [英] How do I implement previous/next month buttons and show dates for current month?
问题描述
场景:
我有一个费用跟踪iOS应用程序,我将费用明细视图控制器中的费用存储到一个表视图中(带有获取的结果控制器),该视图显示费用列表以及类别,金额和日期.我的实体金钱"中确实有一个日期属性,该属性是费用或收入的父实体.
I have an expense tracking iOS Application and I am storing expenses from a expense detail view controller into a table view (with fetched results controller) that shows the list of expenses along with the category and amount and date. I do have a date attribute in my entity "Money" which is a parent entity for either an expense or an income.
问题:
我想要的是基本上每月对费用进行分类,并将其显示为部分标题,例如:(2012年11月1日至11月30日),并显示该月的费用金额和相关内容.该视图中提供了两个按钮,如果我按向右按钮,它将使月份增加一个月(2012年12月1日至12月31日),类似地,向左按钮将使月份减少一个月.
What I want is to basically categorize my expenses on a monthly basis and display it as the section header title for example : (Nov 1 - Nov 30,2012) and it shows expenses amount and related stuff according to that particular month. Two buttons are provided in that view, if I would press the right button, it will increment the month by a month (Dec 1 - Dec 31, 2012) and similarly the left button would decrement the month by a month.
我该怎么做?我正在尝试以下代码-有点工作.假设我当前的节标题为(Nov1-2012年11月30日),当我按向左按钮时,它给我的节标题为(2012年10月1日-10月30日),这是错误的,应该是2012年10月31日而不是2012年10月30日.
How would I accomplish that? I am trying the following code - which is kinda working. Suppose I have my current section header as (Nov1 - Nov 30, 2012) and when I press the left button, it gives me the section header (Oct 1 - Oct 30, 2012) which is wrong, it should be Oct 31, 2012 and not Oct 30, 2012.
- (NSDate *)firstDateOfTheMonth
{
self.startDate = [NSDate date];
NSCalendar* calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:self.startDate];
[components setDay:1];
firstDateOfTheMonth = [[calendar dateFromComponents:components] retain];
return firstDateOfTheMonth;
}
- (NSDate *)lastDateOfTheMonth
{
NSCalendar* calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:firstDateOfTheMonth];
[components setMonth:[components month]+1];
[components setDay:0];
lastDateOfTheMonth = [[calendar dateFromComponents:components] retain];
return lastDateOfTheMonth;
}
然后,我有一个名为"monthCalculation"的方法,其中调用了上述两种方法.
Then I have a method called "monthCalculation" in which I call the above two methods.
- (void)monthCalculation
{
[self firstDateOfTheMonth];
[self lastDateOfTheMonth];
}
当我按下鼠标左键(将月份减少一个月)时,现在显示以下代码:
Now the following code when I press the left button (to decrement the month by a month):
- (IBAction)showPreviousDates:(id)sender
{
[self monthCalculation];
NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease];
[dateComponents setMonth:-1];
NSDate *newDate1 = [[NSCalendar currentCalendar]
dateByAddingComponents:dateComponents
toDate:firstDateOfTheMonth options:0];
NSDate *newDate2 = [[NSCalendar currentCalendar]
dateByAddingComponents:dateComponents
toDate:lastDateOfTheMonth options:0];
self.startDate = newDate1;
self.endDate = newDate2;
NSLog(@" self start date in previous mode =%@", self.startDate);
NSLog(@" self end date in previous mode =%@", self.endDate);
}
当我按下右键(将一个月增加一个月)时,以下代码:
The following code when I press the right button (to increment the month by a month):
- (IBAction)showNextDates:(id)sender
{
[self monthCalculation];
NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease];
[dateComponents setMonth:1];
NSDate *newDate1 = [[NSCalendar currentCalendar]
dateByAddingComponents:dateComponents
toDate:firstDateOfTheMonth options:0];
NSDate *newDate2 = [[NSCalendar currentCalendar]
dateByAddingComponents:dateComponents
toDate:lastDateOfTheMonth options:0];
self.startDate = newDate1;
self.endDate = newDate2;
NSLog(@" self start date in previous mode =%@", self.startDate);
NSLog(@" self end date in previous mode =%@", self.endDate);
}
我做对了吗?还是有更好的方法来实现这一目标?任何帮助将不胜感激.
Am I doing it right? or there is a better way to do achieve this? Any help will be appreciated.
推荐答案
以下是一些应做您想做的方法:
Here are some methods that should do what you want:
首先,在您的viewDidLoad方法中添加:
First, in your viewDidLoad method add:
self.startDate = [NSDate date];
[self updateDateRange];
这为您提供了一个起点.然后,我添加了以下五种方法(注意:previousMonthButtonPressed
/nextMonthButtonPressed
应该连接到您的按钮):
This gives you a starting point. Then, I added the following five methods (Note: previousMonthButtonPressed
/nextMonthButtonPressed
should be wired up to your buttons):
// Return the first day of the month for the month that 'date' falls in:
- (NSDate *)firstDayOfMonthForDate:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:date];
comps.day = 1;
return [cal dateFromComponents:comps];
}
// Return the last day of the month for the month that 'date' falls in:
- (NSDate *)lastDayOfMonthForDate:(NSDate *)date
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:date];
comps.month += 1;
comps.day = 0;
return [cal dateFromComponents:comps];
}
// Move the start date back one month
- (IBAction)previousMonthButtonPressed:(id)sender {
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:self.startDate];
comps.month -= 1;
self.startDate = [cal dateFromComponents:comps];
[self updateDateRange];
}
// Move the start date forward one month
- (IBAction)nextMonthButtonPressed:(id)sender {
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
fromDate:self.startDate];
comps.month += 1;
self.startDate = [cal dateFromComponents:comps];
[self updateDateRange];
}
// Print the new range of dates.
- (void)updateDateRange
{
NSLog(@"First day of month: %@", [self firstDayOfMonthForDate:self.startDate]);
NSLog(@"Last day of month: %@", [self lastDayOfMonthForDate:self.startDate]);
}
这篇关于如何实现上个月/下个月的按钮并显示当月的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!