Control layer
#import "ViewController.h"#import "CZKeyboardToolbar.h"@interfaceViewcontroller () <CZKeyboardToolbarDelegate>@property (Strong, nonatomic) Uidatepicker*DatePicker, @property (weak, nonatomic) Iboutlet Uitextfield*TextField;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib. //Create DatapikcerSelf.datepicker =[[Uidatepicker alloc] init]; //this placeSelf.datepicker.locale = [[Nslocale alloc] Initwithlocaleidentifier:@"ZH"]; //Date control FormatSelf.datepicker.datePickerMode =uidatepickermodedate; //Setting the TextField keyboardSelf.textField.inputView =Self.datepicker; Czkeyboardtoolbar*toolbar =[Czkeyboardtoolbar toolbar]; //set up a proxy for the keyboardToolbar.kbdelegate =Self ; //to set the TextField toolbar for a barSelf.textField.inputAccessoryView =toolbar;}#pragmaMark's Custom Keyboard toolbar proxy Method-(void) Keyboardtoolbar: (Czkeyboardtoolbar *) toolbar btndidselected: (Uibarbuttonitem *) item{if(Item.tag = =2) {//Done button click//get date displayed in TextFieldNSDate *date =self.datepicker.date; NSLog (@"%@", date); //Date to StringNSDateFormatter *dateformatter =[[NSDateFormatter alloc] init]; //Set Date formatDateformatter.dateformat =@"YYYYMMDD"; NSString*datestr =[Dateformatter stringfromdate:date]; Self.textField.text=Datestr;
Hide keyboard
[self.birthdayfiled Resignfirstresponder];
} }/** * Code created by toolbar*/-(void) codefortoolbar{//Code Creation UitoolbarUitoolbar *toolbar =[[Uitoolbar alloc] init]; Toolbar.backgroundcolor=[Uicolor Graycolor]; //Screen WidthCGFloat Screenw =[[UIScreen mainscreen] bounds].size.width; Toolbar.bounds= CGRectMake (0,0, Screenw, -); Uibarbuttonitem*PREVIOUSBTN = [[Uibarbuttonitem alloc] Initwithtitle:@"previous"style:uibarbuttonitemstyleplain Target:nil Action:nil]; Uibarbuttonitem*NEXTBTN = [[Uibarbuttonitem alloc] Initwithtitle:@"Next"style:uibarbuttonitemstyleplain Target:nil Action:nil]; Uibarbuttonitem*DONEBTN = [[Uibarbuttonitem alloc] Initwithtitle:@" Done"style:uibarbuttonitemstyleplain Target:nil Action:nil]; //fixed-length buttonsUibarbuttonitem *fixedbtn =[[Uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemfixedspace target:nil Action:nil]; //code implementation to set the widthFixedbtn.width =Ten; //stretchable ButtonsUibarbuttonitem *flexible =[[Uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemflexiblespace target:nil Action:nil] ; //Add a button inside the UitoolbarToolbar.items =@[previousbtn,fixedbtn,nextbtn,flexible,donebtn];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
Czkeyboardtoolbar.m
#import "CZKeyboardToolbar.h"@interfaceCzkeyboardtoolbar ()@end@implementationCzkeyboardtoolbar/*//Only override drawrect:if your perform custom drawing.//an empty implementation adversely affects performance Duri ng animation.-(void) DrawRect: (cgrect) Rect {//Drawing code}*/+(instancetype) toolbar{return[[[NSBundle Mainbundle] loadnibnamed:@"Czkeyboardtoolbar"Owner:nil Options:nil] lastobject];}-(Ibaction) Itembtnclick: (ID) Sender {//determine if the agent has an implementation method if([self.kbdelegate respondstoselector: @selector (keyboardtoolbar:btndidselected:)]) {[Self.kbdelegate KeyboardT Oolbar:self Btndidselected:sender]; } }@end
CZKeyboardToolbar.h
#import<UIKit/UIKit.h>@classCzkeyboardtoolbar;@protocolCzkeyboardtoolbardelegate <NSObject>@optional/** * Item.tag 0 means Previous button 1: Next button 2:done Finish button*/-(void) Keyboardtoolbar: (Czkeyboardtoolbar *) toolbar btndidselected: (Uibarbuttonitem *) Item;@end@interfaceCzkeyboardtoolbar:uitoolbar+(instancetype) toolbar; @property (weak, nonatomic)ID<CZKeyboardToolbarDelegate> kbdelegate;//Agent for keyboard@end
iOS 11th day (4:uidatepicker time selection, and keyboard handling, loading xib files, proxy mode)