At last:
Main.storyboard
Keyboardtool.xib
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvchjlx2vtaw5lbnq=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
KeyboardTool.h
keyboardtool.h// Keyboard processing// Created by Beyond on 14-8-24.// Copyright (c) 2014 Com.beyond. All rights reserved. #import <UIKit/UIKit.h> @protocol keyboardtooldelegate;typedef enum { Kkeyboardtoolbuttontypenext,//Next button kkeyboardtoolbuttontypeprevious,//Previous button Kkeyboardtoolbuttontypedone//Finished button} Keyboardtoolbuttontype; @interface keyboardtool:uitoolbar// Previous Button control @property (nonatomic, weak) Iboutlet uibarbuttonitem *previousbtn;//Next button control @property (nonatomic, weak) Iboutlet uibarbuttonitem *nextbtn;//Complete button control @property (nonatomic, weak) Iboutlet Uibarbuttonitem *doneBtn;// Agents generally use weak, at the same time, to avoid delegate conflicts with the default inheritance @property (nonatomic, weak) Id<keyboardtooldelegate> tooldelegate;//class method Returns an instance object + (ID) keyboardtool;//The three button click event on the Monitor toolbar-(ibaction) previousbtnclicked;-(ibaction) nextbtnclicked;-( ibaction) donebtnclicked; @end
keyboardtool.m
keyboardtool.m//keyboard processing//Created by Beyond on 14-8-24.//Copyright (c) 2014 Com.beyond. All rights reserved. #import "KeyboardTool.h" #import "KeyboardToolDelegate.h" @implementation keyboardtool//class method, Initialize a keyboardtool+ (ID) from the xib file Keyboardtool {//owner can keyboardtool this class//click on "Next" button. To call the owner's Next method Nsarray *array = [[NSBundle mainbundle] loadnibnamed:@ "Keyboardtool" Owner:nil Options:nil]; Returns the initialized Keyboardtool object return array[0];} #pragma mark-click event//Click on the previous button-(void) previousbtnclicked {if ([_tooldelegate respondstoselector: @selector (keyboard Tool:buttontype:)] {//Tell the agent, click on the Previous button [_tooldelegate keyboardtool:self Buttontype:kkeyboardtoolbuttont Ypeprevious]; }}//clicked the next button-(void) nextbtnclicked {if ([_tooldelegate respondstoselector: @selector (keyboardtool:buttontype:)] {//Tell the agent, click on the Next button [_tooldelegate keyboardtool:self buttontype:kkeyboardtoolbuttontypenext]; }}//Click Finish button-(void) donebtnclicked {if ([_tooldelegate respondstoselector: @selector (keyboardtool:buttontype:)]) {//Tell the agent, click Finish button [_tooldelegate keyboardtool:self Buttontype:kkeyboardtoolbuttontypedone]; }} @end
KeyboardToolDelegate.h
keyboardtooldelegate.h// 22_ keyboard synthesis case//// Created by Beyond on 14-8-24.// Copyright (c) 2014 Com.beyond. All rights reserved.//#import <Foundation/Foundation.h> @class keyboardtool; @protocol Keyboardtooldelegate <nsobject>-(void) Keyboardtool: (Keyboardtool *) tool ButtonType: (Keyboardtoolbuttontype) type; @end
BeyondViewController.h
beyondviewcontroller.h// 22_ keyboard synthesis case//// Created by Beyond on 14-8-24.// Copyright (c) 2014 Com.beyond. All rights reserved.//#import <UIKit/UIKit.h> #import "KeyboardTool.h" #import " KeyboardToolDelegate.h "@interface Beyondviewcontroller:uiviewcontroller <uitextfielddelegate, Uipickerviewdatasource, uipickerviewdelegate, keyboardtooldelegate>//birthday input box @property (weak, nonatomic) IBOutlet Uitextfield *birthdaytextfield;//City Input Box @property (weak, nonatomic) Iboutlet Uitextfield *citytextfield; @end
Beyondviewcontroller.m
beyondviewcontroller.m//22_ keyboard synthesis case////Created by Beyond on 14-8-24.//Copyright (c) 2014 Com.beyond. All rights reserved.//#import "BeyondViewController.h" @interface Beyondviewcontroller ()//All provinces are composed of an array @property ( Nonatomic, strong) Nsarray *provincenamearr;//dictionary: Save name Key---Value city name array @property (nonatomic, strong) Nsdictionary * provincename_cities_dict;//input box for active state currently active @property (nonatomic, weak) Uitextfield *currenttextfield;// The toolbar on the keyboard @property (nonatomic, weak) Keyboardtool *tool;//An array of all input box controls @property (Nonatomic, strong) Nsmutablearray * Alltextfields; @end @implementation beyondviewcontroller-(void) viewdidload{[Super Viewdidload]; Self.alltextfields = [Nsmutablearray array]; Class method, instantiating a Keyboardtool object self.tool = [Keyboardtool Keyboardtool]; Self.tool.backgroundColor = [Uicolor Clearcolor]; Self.tool.barTintColor = [Uicolor Lightgraycolor]; and set the Keyboard tool agent for the current controller, to receive its internal BTN Click events, perceiving btntype self.tool.toolDelegate = self; 1. Keyboard tool to set all text boxesKeyboardtool for (Uitextfield *field in self.view.subviews) {//Assuming it is not a text input box, continue if (![ Field Iskindofclass:[uitextfield class]]) continue; The keyboard tool for each text input box is it ... Field.inputaccessoryview = self.tool;//array holds all text input box controls, followed by [Self.alltextfields Addob Ject:field]; The proxy setting for each text input box is the current controller field.delegate = self; }//2. For the Birthday input box, set the keyboard to DatePicker [self setinputviewforbirthdaytextfield]; 3. For the city input box, set the keyboard to DatePicker [self setinputviewforcitytextfield]; 4. Load the full data [self loadalldata]; }//2. For the Birthday input box, set the keyboard to datepicker-(void) setinputviewforbirthdaytextfield{//Set the keyboard for birthdays (without setting the width height and position) Uidatepicker *datepicker = [[Uidatepicker alloc] init]; Set the region to Chinese Simplified datepicker.locale = [[Nslocale alloc] initwithlocaleidentifier:@ "ZH_CN"]; The mode is: Only show date Datepicker.datepickermode = Uidatepickermodedate; Monitor DatePicker value Change event [DatePicker addtarget:self Action: @selector (datepickervaluechangeed:) ForcontroLevents:uicontroleventvaluechanged]; Set it as the Birthday input box for view Self.birthdayTextField.inputView = DatePicker;} 3. For the city input box, set the keyboard to datepicker-(void) setinputviewforcitytextfield{//Set the city's keyboard uipickerview *picker = [[Uipickerview al LOC] init]; Set data source and proxy picker.datasource = self; Picker.delegate = self; Display indicator picker.showsselectionindicator = YES; Set it as the city input box for view Self.cityTextField.inputView = picker;} 4. Load all data-(void) loadalldata{//Load provinces data nsdictionary *dict = [Nsdictionary Dictionarywithcontentsoffile:[[nsbundl E Mainbundle] pathforresource:@ "Cities" oftype:@ "plist"]; An array of all Self.provincenamearr = dict[@ "Provinces"]; Dictionary, the key is the province name, the value is an array of city names Self.provincename_cities_dict = dict[@ "Cities"];} 2.1 Listening for the birthday selection control Value Change event, assigning a value to the Birthday input box-(void) Datepickervaluechangeed: (Uidatepicker *) picker{nsdateformatter *formatter = [[ NSDateFormatter alloc] init]; Formatter.dateformat = @ "YYYY-MM-DD"; Birthday Input Box Assignment self.birthdayTextField.text = [Formatter StRingFromDate:picker.date];} #pragma mark-pickerview Data source method//Total number of columns-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) pickerview{//The first column is province Name, the second column is the province-name corresponding city array return 2;} The corresponding number of rows per column-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) component{if ( Component = = 0) {//return the length of the province an array group return self.provinceNameArr.count; } else {//returns the 1th column of the currently selected line number Nsuinteger rowNum = [Pickerview selectedrowincomponent:0]; First from the province an array group, take out the corresponding province name NSString *pname = Self.provincenamearr[rownum]; Then from the dictionary, get the city array by province name, and return its length nsarray *cityarr = Self.provincename_cities_dict[pname]; return cityarr.count; }}//what data is displayed for each row in each column-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) row forcomponent: ( Nsinteger) component{if (component = = 0) {//previous column, display province name return Self.provincenamearr[row]; } else {//returns the 1th column of the currently selected line number Nsuinteger rowNum = [Pickerview selectedrowincomponent:0]; First from the province an array group, take out the corresponding province name NSString *pname = Self.provincenamearr[rownum]; Then from the dictionary, get the city array by province name, and return its length nsarray *cityarr = Self.provincename_cities_dict[pname]; return Cityarr[row]; }}//Uipickerview selected a row will be called-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) row incomponent: ( Nsinteger) component{//Refresh after a column of data, linkage effect [Pickerview reloadcomponent:1]; Nsuinteger prownum = [Pickerview selectedrowincomponent:0]; First from the province an array group, take out the corresponding province name NSString *pname = Self.provincenamearr[prownum]; Nsuinteger crownum = [Pickerview selectedrowincomponent:1]; Then from the dictionary, by the province name, get the city array, and return its corresponding city name Nsarray *cityarr = Self.provincename_cities_dict[pname]; NSString *cname = Cityarr[crownum]; City Input Box Assignment value Self.cityTextField.text = [NSString stringwithformat:@ "%@%@", PName, CName];} #pragma mark-Key!!!!!!!! Keyboardtool Proxy Method-(void) Keyboardtool: (Keyboardtool *) tool ButtonType: (Keyboardtoolbuttontype) type{if (type = = KkeyboardtooLbuttontypedone) {//When clicked, the current active input box, cancel the first responder, exit the keyboard [Self.currenttextfield Resignfirstresponder]; } else {//First take out the index of the current input box in the input box array, Nsuinteger index = [Self.alltextfields IndexOfObject:self.currentTextField]; if (type = = kkeyboardtoolbuttontypeprevious) {//When the previous hit, the index minus 1, index--; } else {//when clicking Next, Index plus 1, index++; }//take out the input box of the corresponding index, become the first responder, bring up the corresponding keyboard Uitextfield *field = Self.alltextfields[index]; [Field Becomefirstresponder]; }} #pragma mark-Focus!!!!!!! Uitextfield Proxy Method-(void) textfielddidbeginediting: (Uitextfield *) textfield{//Remember the text box being activated, other methods Keyboardtool:buttonclic K: To use the Self.currenttextfield = TextField; First get the index of this input box in an array of all input boxes Nsuinteger index = [self.alltextfields indexofobject:textfield];//Sets the Next button, is available Self.tool . nextbtn.enabled = Index! = self.alltextfields.count-1;//Sets whether the previous button is available self.tool.previousBtn.enabled = index! = 0; }//This is a way to entrust Uitextfield! //Use this to entrust us when we open the keyboard. Click Return to close the keyboard-(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{//End all edits, exit all keyboards, and return yes to [Self.view EndE Diting:yes]; return YES;} @end
Data Source
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvchjlx2vtaw5lbnq=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Ios_22 Customizing the keyboard toolbar