自定义标签栏按钮中的日期未显示

时间:2018-01-17 16:59:57

标签: ios objective-c tabs

我是iOS的初学者。我想在我的自定义标签的按钮标签上显示当前日期+ 6天。我使用this来构建自定义标签。附件是我的代码的一部分,为什么buttonText没有拿起我的setTitle?是因为它是一个NSMutable数组,请帮忙,谢谢

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

更重要的是,你能教我如何修剪日期字符串吗?只有1月19日星期五?是否可以追加\ n来获得1月19日\ nFri?所有的帮助都是最重要的。提前谢谢。

1 个答案:

答案 0 :(得分:0)

对于按钮标题,我认为你必须使用NSString而不是NSDate。使用NSDateFormatter将NSDate转换为NSString:

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

此外,如果您需要其他日期格式,则可以使用此网站nsdateformatter.com

结果: enter image description here