iOS 11th day (4:uidatepicker time selection, and keyboard handling, loading xib file, proxy mode)

Source: Internet
Author: User
Tags uikit

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.