六.警告框與時間選擇器的完美配合

點擊按鈕 要求:
(1)早上8.00到下午18.00 之內(nèi)可選擇時間 之外不允許
(2)小于當前時間的不能選

-(void)Time
{
    //獲取系統(tǒng)時間
    
    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(@"在時間范圍內(nèi)");
        
        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];
            //選中最小時間是現(xiàn)在
            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(@"不在時間范圍內(nèi)");
    }
}```
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容