IOS第11天(4:UIDatePicker时间选择,和键盘处理,加载xib文件,代理模式)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了IOS第11天(4:UIDatePicker时间选择,和键盘处理,加载xib文件,代理模式),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3703字,纯文字阅读大概需要6分钟。
内容图文
***控制层
#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 == 2) {//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(0, 0, screenW, 44); 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 = 10; //可拉伸的按钮 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
原文:http://www.cnblogs.com/ios-g/p/4742795.html
内容总结
以上是互联网集市为您收集整理的IOS第11天(4:UIDatePicker时间选择,和键盘处理,加载xib文件,代理模式)全部内容,希望文章能够帮你解决IOS第11天(4:UIDatePicker时间选择,和键盘处理,加载xib文件,代理模式)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。