Ios 如何在目标C中有效地迭代一年中的几个月
我在一年中的几个月里迭代,当我到了1月,回到12月,这一年也会更新。下面概述了我为此创建的方法 我正在将日期数据暂时保存到UserDefaults中,当调用ViewWillExample方法时,我访问UserDefaults。当用户使用VIEWDIDDISEAR时,我将使用更新的日期、月份和年份数据保存用户默认值 当用户选择递增月份或递减月份按钮时,UILabel大约需要1-2秒来更新月份名称,这两个按钮都调用此方法Ios 如何在目标C中有效地迭代一年中的几个月,ios,objective-c,nsdate,nsuserdefaults,Ios,Objective C,Nsdate,Nsuserdefaults,我在一年中的几个月里迭代,当我到了1月,回到12月,这一年也会更新。下面概述了我为此创建的方法 我正在将日期数据暂时保存到UserDefaults中,当调用ViewWillExample方法时,我访问UserDefaults。当用户使用VIEWDIDDISEAR时,我将使用更新的日期、月份和年份数据保存用户默认值 当用户选择递增月份或递减月份按钮时,UILabel大约需要1-2秒来更新月份名称,这两个按钮都调用此方法 - (void)shiftHistoricaldateWindowFromBu
- (void)shiftHistoricaldateWindowFromButtonPress:(id)sender
{
UIButton *selectedButton = (UIButton *)sender;
if (selectedButton.tag == 679) {
// view history (backwards)
_monthInt = _monthInt-1;
if (_monthInt < 1) {
_monthInt = 12;
_yearInt = _yearInt - 1;
}
//enable button?
} else {
// view present (forward)
// get present day
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger currentDayInteger = [dateComponents day];
NSInteger currentMonthInteger = [dateComponents month];
NSInteger currentYearInteger = [dateComponents year];
if ((_monthInt != currentMonthInteger) || (_yearInt != currentYearInteger)) {
_monthInt = _monthInt+1;
if (_monthInt > 12) {
_monthInt = 1;
_yearInt = _yearInt + 1;
}
} else {
// disable button?
}
}
// update view
// Read current date of historic graph (stored in NSUserDefaults)
NSDateFormatter *dff = [[NSDateFormatter alloc] init];
// set DateRageLabel
_completeRangeString = [[dff monthSymbols] objectAtIndex:(_monthInt-1)];
_dateRangeLabel.text = _completeRangeString;
}
-(void)将ThistOricalDateWindowFromButton按:(id)发件人
{
UIButton*selectedButton=(UIButton*)发送方;
如果(selectedButton.tag==679){
//查看历史记录(向后)
_monthInt=_monthInt-1;
如果(_<1){
_monthInt=12;
_yearInt=_yearInt-1;
}
//启用按钮?
}否则{
//当前视图(前进)
//得到今天
NSDateComponents*dateComponents=[[NSCalendar currentCalendar]组件:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger currentDayInteger=[dateComponents day];
NSInteger currentmonthintager=[dateComponents-month];
NSInteger currentYearInteger=[dateComponents year];
如果(_monthInt!=CurrentMonthinger)| |(_yearInt!=currentYearInteger)){
_monthInt=_monthInt+1;
如果(_monthInt>12){
_monthInt=1;
_yearInt=_yearInt+1;
}
}否则{
//禁用按钮?
}
}
//更新视图
//读取历史图表的当前日期(存储在NSUserDefaults中)
NSDateFormatter*dff=[[NSDateFormatter alloc]init];
//设置日期标签
_completeRangeString=[[dff monthSymbols]对象索引:(_monthInt-1)];
_dateRangeLabel.text=\u completeRangeString;
}
您显示的代码没有什么慢的地方。问题可能在别处。但是使用仪器来观察发生了什么!这就是它的目的。通常这种规模的延迟是由于在非主队列上调用UIKit方法,而不是由于实际的处理速度。首先,我要确保这是您遇到问题时调用的代码。我还要确保您没有在其他地方访问此标签,特别是在后台队列中。您所显示的代码没有任何慢的地方。问题可能在别处。但是使用仪器来观察发生了什么!这就是它的目的。通常这种规模的延迟是由于在非主队列上调用UIKit方法,而不是由于实际的处理速度。首先,我要确保这是您遇到问题时调用的代码。我还要确保您没有在其他地方访问此标签,特别是在后台队列中。