***控制层

#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. json返回数据库的时间格式为/Date(1477294037000)/,怎样在前台进行格式化转换

    方法一: 原理是取中间的数,再转换成js的Date类型 function ChangeDateFormat(val) { if (val != null) { var date = new Date( ...

  2. css3 -- 背景图处理

    1.多背景图片: p{ background-image:url() , url(); background-position:95% 90% , 50% 50%; background-repect ...

  3. 我的c++学习(2)比较两个数字大小

    #include "stdafx.h" #include<iostream> using namespace std; int max(int i, int j){ / ...

  4. Myeclipse中创建Maven工程的时候没有 webapp-javaee6

    1. http://mvnrepository.com/artifact/org.codehaus.mojo.archetypes/webapp-javaee6/1.5 中有描述

  5. S5中新增的Array方法详细说明

      ES5中新增的Array方法详细说明 by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wor ...

  6. 关于sharedPreferences的使用

    在登录中,android 为什么能够保存上次的登录信息,就是使用了sharedPrerences进行获取和存储 比如记录了之前登录过的用户名.登录时间.登录详情等等

  7. 纯CSS 实现tooltip 内容提示信息效果

    Tooltip 也就是内容的提示信息,合理使用可以给用户比较好的体验. 实现方法有很多种,有很多JS 插件,我这里介绍的是纯CSS实现的方法,兼容性也比较靠谱,IE8+均可正常显示.实现方法也非常简单 ...

  8. display: block; -webkit-margin-before: 1em; -webkit-margin-after: 1em; -webkit-margin-start: 0px; -webkit-margin-end: 0px;

    总的来说:这是CSS3.0的对于文章段P容器的定义方法语句! display:block这个样式,只定义了P容器为一个块; 后面四句是CSS3中的样式定义方法: -webkit-margin-befo ...

  9. 【CodeVS】 p1077 多源最短路

    题目描述 Description 已知n个点(n<=100),给你n*n的方阵,a[i,j]表示从第i个点到第j个点的直接距离. 现在有Q个询问,每个询问两个正整数,a和b,让你求a到b之间的最 ...

  10. 关于transition回调函数的几种写法

    平时工作中经常遇到需要transition动画结束后触发某个功能的问题,但是在映像中好像只见过animate的回调函数, 而transition的很多属性无法在animate中使用,经过一些总结归纳, ...