IOS第11天(4:UIDatePicker时间选择,和键盘处理,加载xib文件,代理模式)
***控制层
#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文件,代理模式)的更多相关文章
- ios – 使用UINib加载xib文件实现UITableViewCell
xib文件的实质是xml,描述界面对象,每个对象都有一个很重要的属性,identity inspector面板中class属性,加载xib文件的时候实际上是实例化界面对象相对应的这些class. xi ...
- 11月26日 用seed,预加载种子文件; Case 条件语句。网址的参数如何传递,; Query--自定义scopes
在seed文件中输入一些预加载的种子job,注意属性和值都要有: ❌错误,我输入contact_email的时候value值是空的,这样不能正确生成. 正确✅: for i in 1..10 do ...
- cordova程序加载pdf文件的2种方法(ios/android)
前言 公司目前的前端架构是微信端由vue全家桶负责h5网站的单页应用,android端和ios端则选择cordova打包成apk和app.其中,有一个业务逻辑是点击某个链接进入pdf的展示,h5的方案 ...
- [转]iOS学习笔记(2)--Xcode6.1创建仅xib文件无storyboard的hello world应用
转载地址:http://www.mamicode.com/info-detail-514151.html 由于Xcode6之后,默认创建storyboard而非xib文件,而作为初学,了解xib的加载 ...
- iOS学习笔记(2)--Xcode6.1创建仅xib文件无storyboard的hello world应用
http://www.mamicode.com/info-detail-514151.html 由于Xcode6之后,默认创建storyboard而非xib文件,而作为初学,了解xib的加载原理很重要 ...
- iOS开发——图形编程Swift篇&CAShapeLayer实现圆形图片加载动画
CAShapeLayer实现圆形图片加载动画 几个星期之前,Michael Villar在Motion试验中创建一个非常有趣的加载动画. 下面的GIF图片展示这个加载动画,它将一个圆形进度指示器和圆形 ...
- Django1.11加载静态文件
Django版本1.11网站通常需要js,css,images等文件,在Django中,我们把这些文件称为“静态文件”(static files).Django提供django.contrib.sta ...
- IOS加载PDF文件
今天的任务是:在iOS上加载显示pdf文件. 方法一:利用webview -(void)loadDocument:(NSString *)documentName inView:(UIWebView ...
- IOS之UI--小实例项目--添加商品和商品名(使用xib文件终结版) + xib相关知识点总结
添加商品和商品名小项目(使用xib文件终结版) 小贴士:博文末尾有项目源码在百度云备份的下载链接. xib相关知识点总结 01-基本使用 一开始使用xib的时候,如果要使用自定义view的代码,就需要 ...
随机推荐
- SpringHttpInvoker解析3-客户端实现
主要的配置文件 <bean id="httpInvokerUserService" class="org.springframework.remoting.http ...
- Swift3.0语言教程字符串转换为数字值
Swift3.0语言教程字符串转换为数字值 Swift3.0语言教程字符串转换为数字值,在NSString中,开发者可以将字符串转换为数字值,通过这些数字值可以实现一些功能,如加法运算.减法运算等.数 ...
- delphi override、overload、reintroduce的区别-0613.txt
http://blog.csdn.net/honglixx/article/details/3624934 1.override overload reintroduce的中文叫法是什么? overr ...
- JS实现类似网页的测试考卷
js实现table中获取不同td的值,并且md5加密,匹配md5的值(避免通过查看网页源代码可以看到正确答案),再用js前端判断输入与正确的值是否相同.最后再把错误的单词计数,并且输出,后端加入对应错 ...
- MongoDB 入门之安装篇
前言:MongoDB 在各 OS 上的安装比较简单,此文章只用来记录,不考虑技术深度. 一.Ubuntu 导入 MongoDB 公钥,添该软件源文件,更新源列表 sudo apt-key adv -- ...
- uva 107 - The Cat in the Hat
The Cat in the Hat Background (An homage to Theodore Seuss Geisel) The Cat in the Hat is a nasty c ...
- CodeForces#275--DIV 2--B(BinarySearch)(!!)
B. Friends and Presents time limit per test 1 second memory limit per test 256 megabytes input stand ...
- 【原】iOS学习之Swift之语法1(精简版)
1. Swift简介 2010年的夏天,苹果公司的开发人员Chris Lattne接到了一个特别的任务,为OS X 和iOS平台开发下一代的编程语言,也就是Swift. 苹果公司于2014年WWDC( ...
- ccc 音乐播放
cc.Class({ extends: cc.Component, properties: { musicPlayer: { default: null, type: cc.AudioSource } ...
- Storm DRPC实现机制分析
DRPC是建立在Storm基本概念(Topology.Spout.Bolt.Stream等)之上的高层抽象,个人理解它的目标是在Storm 集群之上提供一种分布式的RPC框架,以便能够利用Storm快 ...