欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

iOS自定义日期选择器

程序员文章站 2022-05-25 22:57:51
前言封装了一个日期选择器,解决两个问题: 1、点击textfield,键盘弹出和日期选择器弹出的逻辑处理; 2、同一个界面需要多次用到日期选择器时,判断点击的textfield;一、封装日期选择器类y...

前言

封装了一个日期选择器,解决两个问题:
1、点击textfield,键盘弹出和日期选择器弹出的逻辑处理;
2、同一个界面需要多次用到日期选择器时,判断点击的textfield;

一、封装日期选择器类ycdatepickerview

1、新建一个类,基于uiview,取名ycdatepickerview。

2、ycdatepickerview类中.h文件代码如下:

typedef void (^mybasicblock)(id result);

#import <uikit/uikit.h>

@interface ycdatepickerview : uiview

@property (nonatomic, strong) uibutton *btnconfirm;
@property (nonatomic, strong) uibutton *btncancel;
@property (nonatomic, strong) uidatepicker *datepicker;
@property (nonatomic, copy) mybasicblock selectblock;

+ (ycdatepickerview *)datepickerviewwithmode:(uidatepickermode) datepickermode bolck:(mybasicblock)block;


@end

3、ycdatepickerview类中.m文件代码如下:

#define screen_width   [[uiscreen mainscreen] bounds].size.width
#define screen_height   [[uiscreen mainscreen] bounds].size.height

#define ktopbarviewheight    40
#define kbutton_width      40
#define kbutton_height     40
#define kdatepicker_height   256


#import "ycdatepickerview.h"

@implementation ycdatepickerview

- (id)initwithframe:(cgrect)frame
{
  self = [super initwithframe:frame];
  if (self) {

    uiview *topbarview = [[uiview alloc] initwithframe:cgrectmake(0, 0, self.frame.size.width, ktopbarviewheight)];
    topbarview.backgroundcolor = [uicolor orangecolor];
    [self addsubview:topbarview];

    _btnconfirm = [[uibutton alloc] initwithframe:cgrectmake(self.frame.size.width-kbutton_width-10, 0, kbutton_width, kbutton_height)];
    [_btnconfirm addtarget:self action:@selector(btnconfirm:) forcontrolevents:uicontroleventtouchupinside];
    [_btnconfirm settitle:@"确定" forstate:uicontrolstatenormal];
    [topbarview addsubview:_btnconfirm];

    _btncancel = [[uibutton alloc] initwithframe:cgrectmake(10, 0, kbutton_width, kbutton_height)];
    [_btncancel addtarget:self action:@selector(btncancel:) forcontrolevents:uicontroleventtouchupinside];
    [_btncancel settitle:@"取消" forstate:uicontrolstatenormal];
    [topbarview addsubview:_btncancel];

    _datepicker = [[uidatepicker alloc] initwithframe:cgrectmake(0, cgrectgetmaxy(topbarview.frame), self.frame.size.width, self.frame.size.height-ktopbarviewheight)];
    _datepicker.backgroundcolor = [uicolor whitecolor];
    [self addsubview:_datepicker];

  }
  return self;
}

- (void)btnconfirm:(id)sender
{
  if (self.selectblock) {
    self.selectblock(self.datepicker.date);
  }
}

- (void)btncancel:(id)sender
{
  if (self.selectblock) {
    self.selectblock(nil);
  }
}

+ (ycdatepickerview *)datepickerviewwithmode:(uidatepickermode) datepickermode bolck:(mybasicblock)block
{
  ycdatepickerview *picker = [[ycdatepickerview alloc] initwithframe:cgrectmake(0, 0, screen_width, kdatepicker_height)];
  picker.datepicker.datepickermode = datepickermode;
  picker.selectblock = block;
  return picker;
}

@end

二、ycdatepickerview的使用

1、在viewcontroller中导入头文件

#import "ycdatepickerview.h"

2、在viewcontroller.m中添加如下代码

#define screen_width   [[uiscreen mainscreen] bounds].size.width
#define screen_height   [[uiscreen mainscreen] bounds].size.height


#import "viewcontroller.h"
#import "ycdatepickerview.h"

@interface viewcontroller ()

@property (retain, nonatomic) ycdatepickerview *datepicker;

@end

@implementation viewcontroller

- (void)viewdidload {
  [super viewdidload];


  uitextfield *begin = [[uitextfield alloc] initwithframe:cgrectmake(10, 70, screen_width-20, 30)];
  begin.placeholder = @"请输入开始时间";
  begin.borderstyle = uitextborderstyleroundedrect;
  [self.view addsubview:begin];

  __weak viewcontroller *weakself = self;
  begin.inputview = [ycdatepickerview datepickerviewwithmode:uidatepickermodedate
                             bolck:^(nsdate *result) {
                               if (result) {
                                 begin.text = [weakself datetostring:result];
                               }
                               [begin resignfirstresponder];
                             }];


  uitextfield *end = [[uitextfield alloc] initwithframe:cgrectmake(10, 120, screen_width-20, 30)];
  end.placeholder = @"请输入结束时间";
  end.borderstyle = uitextborderstyleroundedrect;
  [self.view addsubview:end];

  end.inputview = [ycdatepickerview datepickerviewwithmode:uidatepickermodedate
                                   bolck:^(nsdate *result) {
                                     if (result) {
                                       end.text = [weakself datetostring:result];
                                     }
                                     [end resignfirstresponder];
                                   }];

}

//日期转为字符串
- (nsstring *)datetostring:(nsdate *)date
{
  nsdateformatter *dateformatter = [[nsdateformatter alloc] init];
  [dateformatter setdateformat:@"yyyy-mm-dd hh:mm:ss"];
  nsstring *strdate = [dateformatter stringfromdate:date];
  return strdate;
}

@end

三、效果图

iOS自定义日期选择器

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: iOS 日期选择器