iOS uipickerview 选择DATE

本文介绍了一种使用自定义UIPickerView组件实现日期选择功能的方法,包括年份、月份和日期的选择,并且能够区分闰年和平年,确保用户在选择日期时的准确性。此外,通过简单的修改,还可以加载时间选择功能。本文详细解释了如何配置UIPickerView的数据源、代理方法以及与其他UI元素的交互,旨在为开发者提供一种实用的日期选择解决方案。

项目中最初使用,后来没有用上放在BLOG中保存,  部分借鉴国外的方法。

这个UIPIKER非常好用,能区分润年,30天和31天的月份。稍加修改能加载时间等。

.h文件

#import <UIKit/UIKit.h>


@interface InvoicesViewController : UIViewController<UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate,UIWebViewDelegate>

{

    


    IBOutlet UIPickerView *CustomPicker; 

    IBOutlet UITextField *timeTextField; //时间选择文本框

    

    IBOutlet UIToolbar *toolbarCancelDone;

    

    IBOutlet UIWebView *checkWebView;

}

@property (nonatomic, strong)NSString *phoneStr;


- (IBAction)actionCancel:(id)sender;


- (IBAction)actionDone:(id)sender;


- (IBAction)checkListBtn:(id)sender;


- (IBAction)backBtn:(id)sender;


.m文件

#import "InvoicesViewController.h"

#define currentMonth [currentMonthString integerValue]

@interface InvoicesViewController ()

{

    NSMutableArray *yearArray;

    NSArray *monthArray;

    NSMutableArray *DaysArray;

    

    NSString *currentMonthString;

    

    int selectedYearRow;

    int selectedMonthRow;

    int selectedDayRow;

    

    BOOL firstTimeLoad;

}

@end

- (void)viewDidLoad

{

    firstTimeLoad = NO;

    CustomPicker.hidden = YES;

    toolbarCancelDone.hidden = YES;


 NSDate *date = [NSDate date];

    

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];

    [formatter setDateFormat:@"yyyy"];

    

    NSString *currentyearString = [NSString stringWithFormat:@"%@",

                                   [formatter stringFromDate:date]];

    // Get Current  Month

    

    [formatter setDateFormat:@"MM"];

    

    currentMonthString = [NSString stringWithFormat:@"%d",[[formatter stringFromDate:date]integerValue]];

    

    // Get Current  Date

    

    [formatter setDateFormat:@"dd"];

    NSString *currentDateString = [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]];

    

     yearArray = [[NSMutableArray alloc]init];

    

    

    for (int i = 1970; i <= 2050 ; i++)

    {

        [yearArray addObject:[NSString stringWithFormat:@"%d",i]];

        

    }


    // PickerView -  Months data

    

    

    monthArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"];

    

    DaysArray = [[NSMutableArray alloc]init];

    

    for (int i = 1; i <= 31; i++)

    {

        [DaysArray addObject:[NSString stringWithFormat:@"%d",i]];

        

    }


    [CustomPicker selectRow:[yearArray indexOfObject:currentyearString] inComponent:0 animated:YES];

    

    [CustomPicker selectRow:[monthArray indexOfObject:currentMonthString] inComponent:1 animated:YES];

    

    [CustomPicker selectRow:[DaysArray indexOfObject:currentDateString] inComponent:2 animated:YES];

    

    

    timeTextField.borderStyle = UITextBorderStyleNone;


}
//PICKER Delegate

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

    

    

    if (component == 0)

    {

        selectedYearRow = row;

        [CustomPicker reloadAllComponents];

    }

    else if (component == 1)

    {

        selectedMonthRow = row;

        [CustomPicker reloadAllComponents];

    }

    else if (component == 2)

    {

        selectedDayRow = row;

        

        [CustomPicker reloadAllComponents];

        

    }

    

}


- (UIView *)pickerView:(UIPickerView *)pickerView

            viewForRow:(NSInteger)row

          forComponent:(NSInteger)component

           reusingView:(UIView *)view {

    

    // Custom View created for each component

    

    UILabel *pickerLabel = (UILabel *)view;

    

    if (pickerLabel == nil) {

        CGRect frame = CGRectMake(0.0, 0.0, 50, 60);

        pickerLabel = [[UILabel alloc] initWithFrame:frame];

        [pickerLabel setTextAlignment:NSTextAlignmentCenter];

        [pickerLabel setBackgroundColor:[UIColor clearColor]];

        [pickerLabel setFont:[UIFont systemFontOfSize:15.0f]];

    }

    

    if (component == 0)

    {

        pickerLabel.text =  [yearArray objectAtIndex:row]; // Year

    }

    else if (component == 1)

    {

        pickerLabel.text =  [monthArray objectAtIndex:row];  // Month

    }

    else if (component == 2)

    {

        pickerLabel.text =  [DaysArray objectAtIndex:row]; // Date

        

    }

    return pickerLabel;

    

}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    return 3;

}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

    

    if (component == 0)

    {

        return [yearArray count];

    }

    else if (component == 1)

    {

        return [monthArray count];

    }

    else if (component == 2)

    { // day

        if (firstTimeLoad)

        {

            if (currentMonth == 1 || currentMonth == 3 || currentMonth == 5 || currentMonth == 7 || currentMonth == 8 || currentMonth == 10 || currentMonth == 12)

            {

                return 31;

            }

            else if (currentMonth == 2)

            {

                int yearint = [[yearArray objectAtIndex:selectedYearRow]intValue ];

                

                if(((yearint %4==0)&&(yearint %100!=0))||(yearint %400==0)){

                    

                    return 29;

                }

                else

                {

                    return 28; // or return 29

                }

                

            }

            else

            {

                return 30;

            }

            

        }

        else

        {

            if (selectedMonthRow == 0 || selectedMonthRow == 2 || selectedMonthRow == 4 || selectedMonthRow == 6 || selectedMonthRow == 7 || selectedMonthRow == 9 || selectedMonthRow == 11)

            {

                return 31;

            }

            else if (selectedMonthRow == 1)

            {

                int yearint = [[yearArray objectAtIndex:selectedYearRow]intValue ];

                

                if(((yearint %4==0)&&(yearint %100!=0))||(yearint %400==0)){

                    return 29;

                }

                else

                {

                    return 28; // or return 29

                }

            }

            else

            {

                return 30;

            }

        }

     }else

     { // am/pm

    return 2;

    

}



}




//点击空白处键盘回退

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    [timeTextField resignFirstResponder];

}



//ToolBarAction

- (IBAction)actionDone:(id)sender {

    timeTextField.text = [NSString stringWithFormat:@"%@-%@-%@",[yearArray objectAtIndex:[CustomPicker selectedRowInComponent:0]],[monthArray objectAtIndex:[CustomPicker selectedRowInComponent:1]],[DaysArray objectAtIndex:[CustomPicker selectedRowInComponent:2]]];

    

    [UIView animateWithDuration:0.5

                          delay:0.1

                        options: UIViewAnimationOptionCurveEaseIn

                     animations:^{

                         

                         CustomPicker.hidden = YES;

                    toolbarCancelDone.hidden = YES;

                         

                         

                     }

                     completion:^(BOOL finished){

                         

                         

                     }];


}


- (IBAction)actionCancel:(id)sender

{

    

    [UIView animateWithDuration:0.5

                          delay:0.1

                        options: UIViewAnimationOptionCurveEaseIn

                     animations:^{

                         

                         CustomPicker.hidden = YES;

                         toolbarCancelDone.hidden = YES;

                         

                         

                     }

                     completion:^(BOOL finished){

                         

                         

                     }];

    

    

}


//textFieldDelegate

- (void)textFieldDidBeginEditing:(UITextField *)textField

{

    [self.view endEditing:YES];

    

}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    

    [UIView animateWithDuration:0.5

                          delay:0.1

                        options: UIViewAnimationOptionCurveEaseIn

                     animations:^{

                         

                         CustomPicker.hidden = NO;

                         toolbarCancelDone.hidden = NO;

                         timeTextField.text = @"";

                         

                     }

                     completion:^(BOOL finished){

                         

                     }];

    CustomPicker.hidden = NO;

    toolbarCancelDone.hidden = NO;

    timeTextField.text = @"";

    return YES;

    

}


- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [textField resignFirstResponder];

    

    return  YES;

    

}





标题基于Python的自主学习系统后端设计与实现AI更换标题第1章引言介绍自主学习系统的研究背景、意义、现状以及本文的研究方法和创新点。1.1研究背景与意义阐述自主学习系统在教育技术领域的重要性和应用价值。1.2国内外研究现状分析国内外在自主学习系统后端技术方面的研究进展。1.3研究方法与创新点概述本文采用Python技术栈的设计方法和系统创新点。第2章相关理论与技术总结自主学习系统后端开发的相关理论和技术基础。2.1自主学习系统理论阐述自主学习系统的定义、特征和理论基础。2.2Python后端技术栈介绍DjangoFlask等Python后端框架及其适用场景。2.3数据库技术讨论关系型和非关系型数据库在系统中的应用方案。第3章系统设计与实现详细介绍自主学习系统后端的设计方案和实现过程。3.1系统架构设计提出基于微服务的系统架构设计方案。3.2核心模块设计详细说明用户管理、学习资源管理、进度跟踪等核心模块设计。3.3关键技术实现阐述个性化推荐算法、学习行为分析等关键技术的实现。第4章系统测试与评估对系统进行功能测试和性能评估。4.1测试环境与方法介绍测试环境配置和采用的测试方法。4.2功能测试结果展示各功能模块的测试结果和问题修复情况。4.3性能评估分析分析系统在高并发等场景下的性能表现。第5章结论与展望总结研究成果并提出未来改进方向。5.1研究结论概括系统设计的主要成果和技术创新。5.2未来展望指出系统局限性并提出后续优化方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值