gpt4 book ai didi

objective-c - 如何实现上个月/下个月按钮并显示当前月份的日期?

转载 作者:行者123 更新时间:2023-11-28 18:12:50 25 4
gpt4 key购买 nike

场景:

我有一个费用跟踪 iOS 应用程序,我将费用从费用详细信息 View Controller 存储到一个 TableView (带有获取的结果 Controller )中,该 TableView 显示费用列表以及类别、金额和日期。我的实体“金钱”中确实有一个日期属性,它是费用或收入的父实体。

问题:

我想要的是基本上按月对我的费用进行分类,并将其显示为部分标题标题,例如:(2012 年 11 月 1 日至 11 月 30 日),它会根据该特定月份显示费用金额和相关内容。该 View 中提供了两个按钮,如果我按下右按钮,它会将月份递增一个月(2012 年 12 月 1 日至 12 月 31 日),同样地,左按钮会将月份递减一个月。

我将如何实现?我正在尝试以下代码 - 这有点工作。假设我当前的部分标题为(2012 年 11 月 1 日 - 11 月 30 日),当我按下左键时,它给了我部分标题(2012 年 10 月 1 日 - 10 月 30 日),这是错误的,它应该是 2012 年 10 月 31 日而不是 2012 年 10 月 30 日。

- (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”的方法,我在其中调用了上述两个方法。

- (void)monthCalculation
{
[self firstDateOfTheMonth];
[self lastDateOfTheMonth];
}

现在,当我按下左键(将月份递减一个月)时,出现以下代码:

- (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);
}

当我按下右键(将月份递增一个月)时的以下代码:

- (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);
}

我做的对吗?还是有更好的方法来实现这一目标?任何帮助将不胜感激。

最佳答案

这里有一些方法可以满足您的需求:

首先,在你的 viewDidLoad 方法中添加:

self.startDate = [NSDate date];
[self updateDateRange];

这为您提供了一个起点。然后,我添加了以下五个方法(注意:previousMonthButtonPressed/nextMonthButtonPressed 应该连接到您的按钮):

// 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]);
}

关于objective-c - 如何实现上个月/下个月按钮并显示当前月份的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13410109/

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