繁体   English   中英

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

[英]Dates in custom Tab bar button not showing

我是iOS的初学者。 我正在尝试在“自定义”标签的按钮标签上显示当前日期+ 6天。 我用来构建我的自定义标签。 附件是我的代码的一部分,为什么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吗? 非常感谢您的帮助。 提前致谢。

对于按钮标题,我认为您必须使用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

结果: 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM