Dates in custom Tab bar button not showing

青春壹個敷衍的年華 提交于 2020-01-06 07:10:08

问题


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.


回答1:


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:



来源:https://stackoverflow.com/questions/48306331/dates-in-custom-tab-bar-button-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!