iOS:求一个 年 月 日 时 分 的DatePicker 时间选择器

发布于 2022-09-02 10:52:51 字数 72 浏览 16 评论 0

如题,系统自带的只能显示年月日,求一个 年 月 日 时 分 的DatePicker 时间选择器,
或者系统自带的可以修改么?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一袭白衣梦中忆 2022-09-09 10:52:52

嫌弃久了是吧 那就来个新的 github一抓一大把现成的 有时间 研究下怎么写的 没时间直接套用就可以了
screen1.png
https://github.com/squimer/DatePickerDialog-iOS-Swift

6597665701958707891.jpg
来源:http://blog.163.com/kanglulu_3309@126/blog/static/189042720121064475492/

 // 头文件 
//
//  DatePicker.h
//
//  Created by Guanglu Kang on 11/06/12.
//  Copyright (c) 2012 x. All rights reserved.
//

#import <UIKit/UIKit.h>
@protocol DatePickerDelegate;
@interface DatePicker : UIView <UIPickerViewDataSource,UIPickerViewDelegate>
// 按照规范, 请把这些设置为外部不可见的
// 不可见的部分, 请放到.m文件里
@property (nonatomic, retain) UIPickerView* yearPicker;     // 年
@property (nonatomic, retain) UIPickerView* monthPicker;    // 月    
@property (nonatomic, retain) UIPickerView* dayPicker;      // 日
@property (nonatomic, retain) UIPickerView* hourPicker;     // 时
@property (nonatomic, retain) UIPickerView* minutePicker;   // 分
// 对外可见的
@property (nonatomic, retain) NSDate*   date;       // 当前date
// 不可见的
@property (nonatomic, retain) UIToolbar*    toolBar;        // 工具条
@property (nonatomic, retain) UILabel*      hintsLabel;     // 提示信息

// 不可见的
@property (nonatomic, retain) NSMutableArray* yearArray;
@property (nonatomic, retain) NSMutableArray* monthArray;
@property (nonatomic, retain) NSMutableArray* dayArray;
@property (nonatomic, retain) NSMutableArray* hourArray;
@property (nonatomic, retain) NSMutableArray* minuteArray;
// 不可见的
@property (nonatomic, assign) NSUInteger yearValue;
@property (nonatomic, assign) NSUInteger monthValue;
@property (nonatomic, assign) NSUInteger dayValue;
@property (nonatomic, assign) NSUInteger hourValue;
@property (nonatomic, assign) NSUInteger minuteValue;

/**
 * 设置默认值为当前时间
 */
-(void)resetDateToCurrentDate;


/**
 * 设置提示信息
 */
-(void)setHintsText:(NSString*)hints;

/**
 * 点击确定按钮 // 按照习惯,这个可木有
 */
-(IBAction)actionEnter:(id)sender;

@property (nonatomic, assign) id<DatePickerDelegate>delegate;
@end


@protocol DatePickerDelegate <NSObject>

/**
 * 点击确定后的事件
 */
@optional
-(void)DatePickerDelegateEnterActionWithDataPicker:(DatePicker*)picker;

@end

// 实现文件 
//
//  DatePicker.m
//
//  Created by Guanglu Kang on 11/06/12.
//  Copyright (c) 2012 x. All rights reserved.
//

#import "DatePicker.h"
typedef enum {
    ePickerViewTagYear = 2012,
    ePickerViewTagMonth,
    ePickerViewTagDay,
    ePickerViewTagHour,
    ePickerViewTagMinute
}PickViewTag;

@interface DatePicker (private)
/**
 * 创建数据源
 */
-(void)createDataSource;

/**
 * create month Arrays
 */
-(void)createMonthArrayWithYear:(NSInteger)yearInt month:(NSInteger)monthInt;
@end


@implementation DatePicker

@synthesize delegate;
@synthesize yearPicker, monthPicker, dayPicker, hourPicker, minutePicker;
@synthesize date;
@synthesize yearArray, monthArray, dayArray, hourArray, minuteArray;
@synthesize toolBar, hintsLabel;

@synthesize yearValue, monthValue;
@synthesize dayValue, hourValue, minuteValue;

#pragma mark -
-(void)dealloc{
    [yearArray release];
    [monthArray release];
    [dayArray release];
    [hourArray release];
    [minuteArray release];
    
    [date release];
    [yearPicker release];
    [monthPicker release];
    [dayPicker release];
//    [hourArray release];
//    [minuteArray release];
    
    [toolBar release];
    [hintsLabel release];
    self.delegate = nil;
    [super dealloc];
}


#pragma mark -
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:CGRectMake(0, 0, 320, 260)];
    if (self) {
        // Initialization code
        [self setBackgroundColor:[UIColor blackColor]];
        NSMutableArray* tempArray1 = [[NSMutableArray alloc] initWithCapacity:0];
        NSMutableArray* tempArray2 = [[NSMutableArray alloc] initWithCapacity:0];
        NSMutableArray* tempArray3 = [[NSMutableArray alloc] initWithCapacity:0];
        NSMutableArray* tempArray4 = [[NSMutableArray alloc] initWithCapacity:0];
        NSMutableArray* tempArray5 = [[NSMutableArray alloc] initWithCapacity:0];
        
        [self setYearArray:tempArray1];
        [self setMonthArray:tempArray2];
        [self setDayArray:tempArray3];
        [self setHourArray:tempArray4];
        [self setMinuteArray:tempArray5];
        
        [tempArray1 release];
        [tempArray2 release];
        [tempArray3 release];
        [tempArray4 release];
        [tempArray5 release];
        
        // 更新数据源
        [self createDataSource];
        
        
        // 创建 toolBar & hintsLabel & enter button
        UIToolbar* tempToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
        [self setToolBar:tempToolBar];
        [tempToolBar release];
        [self addSubview:self.toolBar];
//        [toolBar setTintColor:[UIColor lightTextColor]];
        
        UILabel* tempHintsLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 250, 34)];
        [self setHintsLabel:tempHintsLabel];
        [tempHintsLabel release];
        [self.hintsLabel setBackgroundColor:[UIColor clearColor]];
        [self addSubview:self.hintsLabel];
        [self.hintsLabel setFont:[UIFont systemFontOfSize:24.0f]];
        [self.hintsLabel setTextColor:[UIColor whiteColor]];
        
        
        UIButton* tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [tempBtn setBackgroundImage:[UIImage imageNamed:@"Resourse.bundle/btnNormal.png"] forState:UIControlStateNormal];
        [tempBtn setBackgroundImage:[UIImage imageNamed:@"Resourse.bundle/btnPressed.png"] forState:UIControlStateNormal];
        [tempBtn setTitle:@"确定" forState:UIControlStateNormal];
        [tempBtn sizeToFit];
        [self addSubview:tempBtn];
        [tempBtn setCenter:CGPointMake(320-15-tempBtn.frame.size.width*.5, 22)];
        [tempBtn addTarget:self action:@selector(actionEnter:) forControlEvents:UIControlEventTouchUpInside];
        
        
        // 初始化各个视图
        UIPickerView* yearPickerTemp = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 80, 216)];
        [self setYearPicker:yearPickerTemp];
        [yearPickerTemp release];
        [self.yearPicker setFrame:CGRectMake(0, 44, 80, 216)];
        
        UIPickerView* monthPickerTemp = [[UIPickerView alloc] initWithFrame:CGRectMake(81, 44, 60, 216)];
        [self setMonthPicker:monthPickerTemp];
        [monthPickerTemp release];
        [self.monthPicker setFrame:CGRectMake(80, 44, 61, 216)];
        
        UIPickerView* dayPickerTemp = [[UIPickerView alloc] initWithFrame:CGRectMake(141, 44, 60, 216)];
        [self setDayPicker:dayPickerTemp];
        [dayPickerTemp release];
        [self.dayPicker setFrame:CGRectMake(141, 44, 59, 216)];
        
        UIPickerView* hourPickerTemp = [[UIPickerView alloc] initWithFrame:CGRectMake(201, 44, 60, 216)];
        [self setHourPicker:hourPickerTemp];
        [hourPickerTemp release];
        [self.hourPicker setFrame:CGRectMake(201, 44, 60, 216)];
        
        UIPickerView* minutesPickerTemp = [[UIPickerView alloc] initWithFrame:CGRectMake(261, 44, 60, 216)];
        [self setMinutePicker:minutesPickerTemp];
        [minutesPickerTemp release];
        [self.minutePicker setFrame:CGRectMake(261, 44, 60, 216)];
        
        
        [self.yearPicker setDataSource:self];
        [self.monthPicker setDataSource:self];
        [self.dayPicker setDataSource:self];
        [self.hourPicker setDataSource:self];
        [self.minutePicker setDataSource:self];
        
        [self.yearPicker setDelegate:self];
        [self.monthPicker setDelegate:self];
        [self.dayPicker setDelegate:self];
        [self.hourPicker setDelegate:self];
        [self.minutePicker setDelegate:self];
        
        
        
        [self.yearPicker setTag:ePickerViewTagYear];
        [self.monthPicker setTag:ePickerViewTagMonth];
        [self.dayPicker setTag:ePickerViewTagDay];
        [self.hourPicker setTag:ePickerViewTagHour];
        [self.minutePicker setTag:ePickerViewTagMinute];
        
        
        [self addSubview:self.yearPicker];
        [self addSubview:self.monthPicker];
        [self addSubview:self.dayPicker];
        [self addSubview:self.hourPicker];
        [self addSubview:self.minutePicker];
        
        [self.yearPicker setShowsSelectionIndicator:YES];
        [self.monthPicker setShowsSelectionIndicator:YES];
        [self.dayPicker setShowsSelectionIndicator:YES];
        [self.hourPicker setShowsSelectionIndicator:YES];
        [self.minutePicker setShowsSelectionIndicator:YES];
        
        
        [self resetDateToCurrentDate];
    }
    return self;
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
//- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
//    if (ePickerViewTagYear ==  pickerView.tag) {
//        return 60.0f;
//    } else {
//        return 40.0f;
//    }
//}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (ePickerViewTagYear == pickerView.tag) {
        return [self.yearArray count];
    }
    
    if (ePickerViewTagMonth == pickerView.tag) {
        return [self.monthArray count];
    }
    
    if (ePickerViewTagDay == pickerView.tag) {
        return [self.dayArray count];
    }
    
    if (ePickerViewTagHour == pickerView.tag) {
        return [self.hourArray count];
    }
    
    if (ePickerViewTagMinute == pickerView.tag) {
        return [self.minuteArray count];
    }
    return 0;
}
#pragma makr - UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (ePickerViewTagYear == pickerView.tag) {
        return [self.yearArray objectAtIndex:row];
    }
    
    if (ePickerViewTagMonth == pickerView.tag) {
        return [self.monthArray objectAtIndex:row];
    }
    
    if (ePickerViewTagDay == pickerView.tag) {
        return [self.dayArray objectAtIndex:row];
    }
    
    if (ePickerViewTagHour == pickerView.tag) {
        return [self.hourArray objectAtIndex:row];
    }
    
    if (ePickerViewTagMinute == pickerView.tag) {
        return [self.minuteArray objectAtIndex:row];
    }
    return @"";
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (ePickerViewTagYear == pickerView.tag) {
        self.yearValue = [[self.yearArray objectAtIndex:row] intValue];
    } else if(ePickerViewTagMonth == pickerView.tag){
        self.monthValue = [[self.monthArray objectAtIndex:row] intValue];
    } else if(ePickerViewTagDay == pickerView.tag){
        self.dayValue = [[self.dayArray objectAtIndex:row]intValue];
    } else if(ePickerViewTagHour == pickerView.tag){
        self.hourValue = [[self.hourArray objectAtIndex:row]intValue];
    } else if(ePickerViewTagMinute == pickerView.tag){
        self.minuteValue = [[self.minuteArray objectAtIndex:row] intValue];
    }
    if (ePickerViewTagMonth == pickerView.tag || ePickerViewTagYear == pickerView.tag) {
        [self createMonthArrayWithYear:self.yearValue month:self.monthValue];
        [self.dayPicker reloadAllComponents];
    }
    
    NSString* str = [NSString stringWithFormat:@"%04d%02d%02d%02d%02d",self.yearValue, self.monthValue, self.dayValue, self.hourValue, self.minuteValue];
    [self setDate:[NSDate dateFromString:str withFormat:@"yyyyMMddHHmm"]];
}
#pragma mark - 年月日闰年=情况分析
/**
 * 创建数据源
 */
-(void)createDataSource{
    // 年
    NSInteger yearInt = [[NSDate date] getYear];
    [self.yearArray removeAllObjects];
    for (int i=yearInt -10; i<=yearInt; i++) {
        [self.yearArray addObject:[NSString stringWithFormat:@"%d",i]];
    }
    
    // 月
    [self.monthArray removeAllObjects];
    for (int i=1; i<=12; i++) {
        [self.monthArray addObject:[NSString stringWithFormat:@"%d",i]];
    }
    

    NSInteger month = [[NSDate date] getMonth];
    
    [self createMonthArrayWithYear:yearInt month:month];

    // 时
    [self.hourArray removeAllObjects];
    for(int i=0; i<24; i++){
        [self.hourArray addObject:[NSString stringWithFormat:@"%02d",i]];
    }

    // 分
    [self.minuteArray removeAllObjects];
    for(int i=0; i<60; i++){
        [self.minuteArray addObject:[NSString stringWithFormat:@"%02d",i]];
    }
}
#pragma mark - 
-(void)resetDateToCurrentDate{
    NSDate* nowDate = [NSDate date];
    [self.yearPicker selectRow:[self.yearArray count]-1 inComponent:0 animated:YES];
    [self.monthPicker selectRow:[nowDate getMonth]-1 inComponent:0 animated:YES];
    [self.dayPicker selectRow:[nowDate getDay]-1 inComponent:0 animated:YES];
    
    [self.hourPicker selectRow:[nowDate getHours] inComponent:0 animated:YES];
    [self.minutePicker selectRow:[nowDate getMinutes] inComponent:0 animated:YES];
    
    
    [self setYearValue:[nowDate getYear]];
    [self setMonthValue:[nowDate getMonth]];
    [self setDayValue:[nowDate getDay]];
    [self setHourValue:[nowDate getHours]];
    [self setMinuteValue:[nowDate getMinutes]];
    
    NSString* str = [NSString stringWithFormat:@"%04d%02d%02d%02d%02d",self.yearValue, self.monthValue, self.dayValue, self.hourValue, self.minuteValue];
    [self setDate:[NSDate dateFromString:str withFormat:@"yyyyMMddHHmm"]];
}
#pragma mark - 
-(void)createMonthArrayWithYear:(NSInteger)yearInt month:(NSInteger)monthInt{
    int endDate = 0;
    switch (monthInt) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            endDate = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            endDate = 30;
            break;
        case 2:
            // 是否为闰年
            if (yearInt % 400 == 0) {
                endDate = 29;
            } else {
                if (yearInt % 100 != 0 && yearInt %4 ==4) {
                    endDate = 29;
                } else {
                    endDate = 28;
                }
            }
            break;
        default:
            break;
    }
    
    if (self.dayValue > endDate) {
        self.dayValue = endDate;
    }
    // 日
    [self.dayArray removeAllObjects];
    for(int i=1; i<=endDate; i++){
        [self.dayArray addObject:[NSString stringWithFormat:@"%d",i]];
    }
}
#pragma mark - 点击确定按钮
-(IBAction)actionEnter:(id)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(DatePickerDelegateEnterActionWithDataPicker:)]) {
        [self.delegate DatePickerDelegateEnterActionWithDataPicker:self];
    }
}
#pragma mark - 设置提示信息
-(void)setHintsText:(NSString*)hints{
    [self.hintsLabel setText:hints];
}
#pragma mark -
-(void)removeFromSuperview{
    self.delegate = nil;
    [super removeFromSuperview];
}
@end

// 使用方法

- (void)viewDidLoad
{
    [super viewDidLoad];

    DatePicker* tempDatePicker = [[DatePicker alloc] init];
    [self.view addSubview:tempDatePicker];
    [tempDatePicker setDelegate:self];
    [tempDatePicker setHintsText:@"拔动选择开始时间"];
    [tempDatePicker setCenter:CGPointMake(self.view.frame.size.width*.5, self.view.frame.size.height-tempDatePicker.frame.size.height*.5)];
    [tempDatePicker release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文