***控制层

#import "ViewController.h"
#import "CZKeyboardToolbar.h" @interface ViewController ()<CZKeyboardToolbarDelegate>
@property (strong, nonatomic) UIDatePicker *datepicker;
@property (weak, nonatomic) IBOutlet UITextField *textField; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. //创建datapikcer
self.datepicker = [[UIDatePicker alloc] init]; //本地方
self.datepicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"]; //日期控件格式
self.datepicker.datePickerMode = UIDatePickerModeDate; //设置textfield的键盘
self.textField.inputView = self.datepicker; CZKeyboardToolbar *toolbar = [CZKeyboardToolbar toolbar]; //设置键盘的代理
toolbar.kbDelegate = self; //设置textfield的辅助工具条
self.textField.inputAccessoryView = toolbar;
} #pragma mark 自定义键盘工具条的代理方法
-(void)keyboardToolbar:(CZKeyboardToolbar *)toolbar btndidSelected:(UIBarButtonItem *)item{ if (item.tag == ) {//Done按钮点击
//获取日期显示在textField
NSDate *date = self.datepicker.date;
NSLog(@"%@",date); //日期转字符串
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//设置日期格式
dateFormatter.dateFormat = @"yyyyMMdd"; NSString *dateStr = [dateFormatter stringFromDate:date]; self.textField.text = dateStr; //隐藏键盘 [self.birthdayFiled resignFirstResponder];
    }

}

/**
* 代码创建的toolbar
*/
-(void)codeForToolbar{
//代码创建UIToolbar
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.backgroundColor = [UIColor grayColor]; //屏幕宽度
CGFloat screenW = [[UIScreen mainScreen] bounds].size.width;
toolbar.bounds = CGRectMake(, , screenW, ); UIBarButtonItem *previousBtn = [[UIBarButtonItem alloc] initWithTitle:@"上一个" style:UIBarButtonItemStylePlain target:nil action:nil]; UIBarButtonItem *nextBtn = [[UIBarButtonItem alloc] initWithTitle:@"下一个" style:UIBarButtonItemStylePlain target:nil action:nil]; UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:nil action:nil]; //固定长度的按钮
UIBarButtonItem *fixedBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
//代码实现要设置宽度
fixedBtn.width = ; //可拉伸的按钮
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; //添加UIToolbar里面的按钮
toolbar.items = @[previousBtn,fixedBtn,nextBtn,flexible,doneBtn];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

***CZKeyboardToolbar.m

#import "CZKeyboardToolbar.h"

@interface CZKeyboardToolbar()

@end

@implementation CZKeyboardToolbar

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
+(instancetype)toolbar{
return [[[NSBundle mainBundle] loadNibNamed:@"CZKeyboardToolbar" owner:nil options:nil] lastObject];
} - (IBAction)itemBtnClick:(id)sender { //判断代理有没有实现方法
if ([self.kbDelegate respondsToSelector:@selector(keyboardToolbar:btndidSelected:)]) {
[self.kbDelegate keyboardToolbar:self btndidSelected:sender];
} }
@end

***CZKeyboardToolbar.h

#import <UIKit/UIKit.h>

@class CZKeyboardToolbar;
@protocol CZKeyboardToolbarDelegate <NSObject> @optional /**
* item.tag 0 表示上一个按钮 1:下一个按钮 2:done完成按钮
*/
-(void)keyboardToolbar:(CZKeyboardToolbar *)toolbar btndidSelected:(UIBarButtonItem *)item; @end @interface CZKeyboardToolbar : UIToolbar +(instancetype)toolbar; @property (weak, nonatomic) id<CZKeyboardToolbarDelegate> kbDelegate;//键盘的代理 @end

IOS第11天(4:UIDatePicker时间选择,和键盘处理,加载xib文件,代理模式)的更多相关文章

  1. ios – 使用UINib加载xib文件实现UITableViewCell

    xib文件的实质是xml,描述界面对象,每个对象都有一个很重要的属性,identity inspector面板中class属性,加载xib文件的时候实际上是实例化界面对象相对应的这些class. xi ...

  2. 11月26日 用seed,预加载种子文件; Case 条件语句。网址的参数如何传递,; Query--自定义scopes

    在seed文件中输入一些预加载的种子job,注意属性和值都要有:  ❌错误,我输入contact_email的时候value值是空的,这样不能正确生成. 正确✅: for i in 1..10 do ...

  3. cordova程序加载pdf文件的2种方法(ios/android)

    前言 公司目前的前端架构是微信端由vue全家桶负责h5网站的单页应用,android端和ios端则选择cordova打包成apk和app.其中,有一个业务逻辑是点击某个链接进入pdf的展示,h5的方案 ...

  4. [转]iOS学习笔记(2)--Xcode6.1创建仅xib文件无storyboard的hello world应用

    转载地址:http://www.mamicode.com/info-detail-514151.html 由于Xcode6之后,默认创建storyboard而非xib文件,而作为初学,了解xib的加载 ...

  5. iOS学习笔记(2)--Xcode6.1创建仅xib文件无storyboard的hello world应用

    http://www.mamicode.com/info-detail-514151.html 由于Xcode6之后,默认创建storyboard而非xib文件,而作为初学,了解xib的加载原理很重要 ...

  6. iOS开发——图形编程Swift篇&CAShapeLayer实现圆形图片加载动画

    CAShapeLayer实现圆形图片加载动画 几个星期之前,Michael Villar在Motion试验中创建一个非常有趣的加载动画. 下面的GIF图片展示这个加载动画,它将一个圆形进度指示器和圆形 ...

  7. Django1.11加载静态文件

    Django版本1.11网站通常需要js,css,images等文件,在Django中,我们把这些文件称为“静态文件”(static files).Django提供django.contrib.sta ...

  8. IOS加载PDF文件

    今天的任务是:在iOS上加载显示pdf文件. 方法一:利用webview -(void)loadDocument:(NSString *)documentName inView:(UIWebView  ...

  9. IOS之UI--小实例项目--添加商品和商品名(使用xib文件终结版) + xib相关知识点总结

    添加商品和商品名小项目(使用xib文件终结版) 小贴士:博文末尾有项目源码在百度云备份的下载链接. xib相关知识点总结 01-基本使用 一开始使用xib的时候,如果要使用自定义view的代码,就需要 ...

随机推荐

  1. Python profiling

    Profiling(性能调试)是我一直很感兴趣的一个话题,之前给大家介绍过Datadog这个工具,今天我们来看看Python语言中有哪些方法来做Profiling. Poorman's Profile ...

  2. NSDICTIONARY获取内容的CRASH

    NSString *path = [self.dataPath stringByAppendingPathComponent:@"dummy.plist"]; NSMutableD ...

  3. Shell 编程基础之基本语法结构汇总

    一.条件语句 简单条件 if [ condition ]; then # 当 condition 成立时,执行内容: fi # 将 if 反过来写,fi 结束 if 之意 复杂条件 if [ cond ...

  4. HTML5属性运用

    HTML5 接触移动端,或专注于支持HTML5浏览器进行前端开发的工作者都不会陌生,这个已经普及很广,对于我专注于PC端开发的人来说,觉得陌生但又觉得很熟悉,大家都知道做PC前端开发为了兼容IE老版本 ...

  5. PLSQL Developer 不能连接 oracle 11g 64位 的解决办法

    http://blog.itpub.net/14184018/viewspace-760730 http://www.cnblogs.com/gulvzhe/archive/2012/08/27/26 ...

  6. topcoder SRM 617 DIV2 SlimeXSlimonadeTycoon

    此题需要注意的两个地方是 (1)在某天生产出来的Slimonades,必须在stale_limit天内必须卖完,否则超过stale_limit内抛弃(东西都有保质期) (2)每天生产出来的Slimon ...

  7. XCODE UITextField 中的属性和用法

    XCODE  UITextField  中的属性和用法 一些基本的用法 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedR ...

  8. 洛谷 洛谷 P1011 车站 Label:续命模拟QAQ 未知50分

    题目描述 火车从始发站(称为第1站)开出,在始发站上车的人数为a,然后到达第2站,在第2站有人上.下车,但上.下车的人数相同,因此在第2站开出时(即在到达第3站之前)车上的人数保持为a人.从第3站起( ...

  9. locale的设定及其LANG、LC_ALL、LANGUAGE环境变量的区别

    locale这个单词中文翻译成地区或者地域,其实这个单词包含的意义要宽泛很多.Locale是根据计算机用户所使用的语言,所在国家或者地区,以及当地的文化传统所定义的一个软件运行时的语言环境. [ora ...

  10. jsonkit mrc于arc混编