六.警告框与时间选择器的完美配合

点击按钮 要求:
(1)早上8.00到下午18.00 之内可选择时间 之外不允许
(2)小于当前时间的不能选

-(void)Time
{
    //获取系统时间
    
    NSDate * senddate=[NSDate date];
    
    
    NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
    
    [dateformatter setDateFormat:@"HH"];
    
    int  locationString =[[dateformatter stringFromDate:senddate] intValue];
    
    //判断时间(8 - 18)
    if (locationString >= 8 && locationString <= 18)
    {
        NSLog(@"在时间范围内");
        
        UIAlertController* alertVc=[UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n" message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
        UIDatePicker* datePicker=[[UIDatePicker alloc]init];
        datePicker.datePickerMode = UIDatePickerModeDateAndTime;
        UIAlertAction* ok=[UIAlertAction actionWithTitle:@"确认" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
            //            nsdate 类型的时间
            NSDate* chosedate=[datePicker date];
            NSDateFormatter* formatter=[[NSDateFormatter alloc]init];
            //选中最小时间是现在
            NSDate *  nowDate=[NSDate date];
            datePicker.minimumDate = nowDate;
            
            
            [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            //            字符串类型的时间
            NSString* curentDatest=[formatter stringFromDate:chosedate];
            //输出选中时间是多少
            NSString *text=curentDatest;
            //选中时间的时间戳是多少
            
            
            NSLog(@"%@",text);
            
            //            选中时间的时间戳
//            NSString *timeSp=[NSString   stringWithFormat:@"%ld",(long)[chosedate timeIntervalSince1970]];
            
            
            if ([nowDate timeIntervalSince1970] - [chosedate timeIntervalSince1970] <0)
            {
                NSLog(@"时间能选");
                
                
            }else
            {
                NSLog(@"时间bu能选");
            
            }
        }];
        UIAlertAction* no=[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:nil];
        [alertVc.view addSubview:datePicker];
        [alertVc addAction:ok];
        [alertVc addAction:no];
        [self presentViewController:alertVc animated:YES completion:nil];
        
    }else
    {
      NSLog(@"不在时间范围内");
    }
}```

你可能感兴趣的:(六.警告框与时间选择器的完美配合)