IOS custom date selector

Time:2021-7-24

preface

Encapsulates a date selector to solve two problems:
1. Click textfield to pop up the logic processing of keyboard and date selector;
2. When the date selector needs to be used multiple times in the same interface, judge the textfield clicked;

1、 Encapsulates the date selector class ycdatepickerview

1. Create a new class based on uiview and named ycdatepickerview.

2. The. H file code in ycdatepickerview class is as follows:


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. The. M file code in ycdatepickerview class is as follows:

#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: @ "OK" 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: @ "Cancel" 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

2、 Use of ycdatepickerview

1. Import header file in viewcontroller


#import "YCDatePickerView.h"

2. Add the following code in 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 = @ "please enter the start time";
  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 = @ "please enter the end time";
  end.borderStyle = UITextBorderStyleRoundedRect;
  [self.view addSubview:end];

  end.inputView = [YCDatePickerView datePickerViewWithMode:UIDatePickerModeDate
                                   bolck:^(NSDate *result) {
                                     if (result) {
                                       end.text = [weakself dateToString:result];
                                     }
                                     [end resignFirstResponder];
                                   }];

}

//Date to string
- (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

3、 Renderings

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

VBS obtains the operating system and its version number

VBS obtains the operating system and its version number ? 1 2 3 4 5 6 7 8 9 10 11 12 ‘************************************** ‘*by r05e ‘* operating system and its version number ‘************************************** strComputer = “.” Set objWMIService = GetObject(“winmgmts:” _  & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”) Set colOperatingSystems = objWMIService.ExecQuery _  (“Select * from […]