问题描述
我是 iOS 初学者.我试图在我的自定义选项卡的按钮标签上显示当前日期 + 6 天.我已经使用
I am a beginner in iOS. I am trying to display the current date + 6days at the button label of my custom tab. I have used this to build my custom tab. Attached is part of my code, why is the buttonText not get pick up my setTitle? Is it because it is a NSMutable Array, please help, thanks
-(void)setupSegmentButtons {
navigationView = [[UIView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.navigationBar.frame.size.height)];
NSInteger numControllers = [viewControllerArray count];
if (!buttonText) {
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *beginningOfThisWeek;
NSTimeInterval durationOfWeek;
[calendar rangeOfUnit:NSWeekCalendarUnit
startDate:&beginningOfThisWeek
interval:&durationOfWeek
forDate:now];
NSMutableArray *buttonText = [@[] mutableCopy];
NSDateComponents *comps = [calendar components:NSUIntegerMax fromDate:beginningOfThisWeek];
for (NSUInteger i = 0; i < 7; ++i) {
[buttonText addObject:[calendar dateFromComponents:comps]];
++comps.day;
}
// buttonText = [[NSArray alloc]initWithObjects: @"first",@"second",@"third",@"fourth",@"etc",@"etc",@"etc",@"etc",nil]; //%%%buttontitle
}
for (int i = 0; i<numControllers; i++) {
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT)];
[navigationView addSubview:button];
button.tag = i; //%%% IMPORTANT: if you make your own custom buttons, you have to tag them appropriately
button.backgroundColor = [UIColor colorWithRed:0.03 green:0.07 blue:0.08 alpha:1];//%%% buttoncolors
[button addTarget:self action:@selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[buttonText objectAtIndex:i] forState:UIControlStateNormal]; //%%%buttontitle
}
pageController.navigationController.navigationBar.topItem.titleView = navigationView;
More over, can you teach me how to trim the date string? to just have Jan 19 Fri? And is it possible to append \n to get Jan 19\nFri? All your help is upmost appreciated. Thanks in advance.
For button title i think you must use NSString instead of NSDate. Convert NSDate to NSString with NSDateFormatter:
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *beginningOfThisWeek;
NSTimeInterval durationOfWeek;
[calendar rangeOfUnit:NSWeekCalendarUnit
startDate:&beginningOfThisWeek
interval:&durationOfWeek
forDate:now];
NSMutableArray *buttonText = [@[] mutableCopy];
NSDateComponents *comps = [calendar components:NSUIntegerMax fromDate:now];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd\n EEE"];
for (int i = 0; i<numControllers; i++) {
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(X_BUFFER+i*(self.view.frame.size.width-2*X_BUFFER)/numControllers-X_OFFSET, Y_BUFFER, (self.view.frame.size.width-2*X_BUFFER)/numControllers, HEIGHT)];
[navigationView addSubview:button];
button.tag = i; //%%% IMPORTANT: if you make your own custom buttons, you have to tag them appropriately
button.backgroundColor = [UIColor colorWithRed:0.03 green:0.07 blue:20 alpha:1];//%%% buttoncolors
button.titleLabel.font = [UIFont systemFontOfSize:12.0];
button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
button.titleLabel.textAlignment = NSTextAlignmentCenter;
[button addTarget:self action:@selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
NSString *dateString = [dateFormatter stringFromDate:[calendar dateFromComponents:comps]];
[buttonText addObject:dateString];
++comps.day;
[button setTitle:[buttonText objectAtIndex:i] forState:UIControlStateNormal]; //%%%buttontitle
Also if you need another date format you can use this site nsdateformatter.com
The RESULT:
这篇关于自定义标签栏按钮中的日期未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!