iOS uipickerview 选择DATE

项目中最初使用,后来没有用上放在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;

    

}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值