禁用按钮

时间:2016-01-01 05:56:37

标签: ios objective-c nsdate nscalendar

我有6个UILabels,它会显示如下日期:

                    Jan,2016 (label)
   (button)  < **27Dec 28Dec 29Dec 30Dec 31Dec 1Jan** >(Button)

当用户可以移动到返回日期或上一个日期时,我在两侧都有两个箭头UIButton。我有一个标签,根据我的日期显示当前月份

这是我的完整代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    firstdate = [NSDate date];
    firstdate = [NSDate dateWithTimeInterval:-(5*86400) sinceDate:firstdate];
    [self dateChange];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MMM,yyyy"];  dateLabel.text = [dateFormat stringFromDate:[NSDate date]];
    dateLabel.text = [dateFormat stringFromDate: firstdate];   
}

-(void)dateChange
{
    NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    dateFormatter.dateFormat = @"ddMMM";
    for (NSInteger i = 0; i < 6; ++i) {
        NSDate *nextDate = [calendar dateByAddingUnit: NSCalendarUnitDay value:i toDate:firstdate options:nil];
        UILabel *label = (UILabel *)labelArray[i];
        label.text = [dateFormatter stringFromDate:nextDate];

        if (i==5) {
            dateFormatter.dateFormat=@"MMM,yyyy";
            dateLabel.text = [[dateFormatter stringFromDate:nextDate] capitalizedString];
        } 
    } 
}
- (IBAction)calRight:(id)sender {
    firstdate = [NSDate dateWithTimeInterval:86400 sinceDate:firstdate];
    [self dateChange];
}

- (IBAction)calLeft:(id)sender {

    firstdate = [NSDate dateWithTimeInterval:-86400 sinceDate:firstdate];
    [self dateChange];
}

所以我需要的内容。例如说 - 今天是Jan 1。如果用户打开我的应用,他们会在Jan 1看到last date,之后是december date像我上面提到的那样。

因此,如果current date上的用户意味着Jan1表示Rightbutton,那么disabled就应该是left button。在用户使用november month转换right button menas的同时,我的enabled应为rightbutton

disabled中的用户时,只有我的current date应为if (some condition) [leftBtn setEnabled:NO]; else [leftBtn setEnabled:YES]; 。 请帮我解决一下我的想法!

 <return type> <function name> (<parameter1,....>){
    <function body>
   }

但我不确定条件和代码。

3 个答案:

答案 0 :(得分:1)

使用波纹管代码更改方法dateChange,并且btnRight是您的右侧按钮,因此请更改您为其提供的任何名称。

-(void)dateChange
{
    NSArray *labelArray = @[flabel, slabel, tlabel, folabel, fivlabel,sixlabel];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    dateFormatter.dateFormat = @"ddMMM";
    for (NSInteger i = 0; i < labelArray.count; ++i) {

        NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:firstdate options:nil];
        UILabel *label = (UILabel *)labelArray[i];
        label.text = [dateFormatter stringFromDate:nextDate];

        if (i==(labelArray.count-1)) {
            dateFormatter.dateFormat=@"MMM,yyyy";
            dateLabel.text = [[dateFormatter stringFromDate:nextDate] capitalizedString];

            NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"yyyy-MM-dd"];

            if ([[dateFormat stringFromDate:nextDate] isEqualToString:[dateFormat stringFromDate:[NSDate date]]])
               btnRight.enabled = false; //It's the same day
            else
               btnRight.enabled = true;
        }
    }
}

答案 1 :(得分:0)

 NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];//You Can Change Date Format
    NSString *strDate=[dateFormatter stringFromDate:[NSDate date]];

    [btnYourButton setEnabled:([strDate isEqualToString:txtYourTextFieldDate.text])?NO:YES];

答案 2 :(得分:0)

//create and set cookies for colors var skinFileName = Cookies.get('skin_file'); //if don't exist cookie for style Load Default stlye if (skinFileName=="") { $('#color_skin').attr('href', '/DNN_test/Portals/_default/Skins/Crew/css/style.css'); } else { $('#color_skin').attr('href', '/DNN_test/Portals/_default/Skins/Crew/css/skinFileName.css'); } //change colors of skin var colors = $('#color_skin'); colors.on('click', function () { //var dataTem = $(this).data('theme'); $('#color_skin').attr('href', '/DNN_test/Portals/_default/Skins/Crew/css/dataTem.css'); Cookies.set('skin_file', dataTem.toString(), { expires: 60, path: '' }); }); 有一个非常方便的方法来检查今天是否包含特定日期。

NSCalendar

重要提示:

永远不要使用86400常量

进行日期数学运算

请注意,在夏令时变化的一天,没有86400秒。

例如,而不是

rightButton.enabled = ![[NSCalendar currentCalendar] isDateInToday:aDate];

firstdate = [NSDate date];
firstdate = [NSDate dateWithTimeInterval:-(5*86400) sinceDate:firstdate];