iOS工作系列控件类iOS Developer

自定义日期键盘

2016-02-20  本文已影响177人  Z了个L
// LZDateTextF.h
#import <UIKit/UIKit.h>

@interface LZDateTextF : UITextField

@end

// LZDateTextF.m
#import "LZDateTextF.h"

@interface LZDateTextF ()

@property(nonatomic, weak)UIDatePicker *pickV;

@end

@implementation LZDateTextF


// 代码创建的时候调用
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

// 从storyboard或者xib中创建的时候调用
- (void)awakeFromNib
{
    [self setup];
}

// 设置
- (void)setup
{
    UIDatePicker *pickV = [[UIDatePicker alloc] init];
    // 日期模式
    pickV.datePickerMode = UIDatePickerModeDate;
    // 日期本地化
    pickV.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
    //修改键盘类型为UIDatePicker
    self.inputView = pickV;
    self.pickV = pickV;

    // 添加事件
    [pickV addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
}

- (void)dateChange:(UIDatePicker *)pickV
{
    //获取当前的日期
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd";
     //把日期改换成字符串
    NSString *dateStr = [fmt stringFromDate:pickV.date];

    self.text = dateStr;
}

@end

效果图片:

上一篇 下一篇

猜你喜欢

热点阅读