Ios_22 Customizing the keyboard toolbar

Source: Internet
Author: User
Tags uikit

Eventually:





Main.storyboard





Keyboardtool.xib




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//Finish 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;//proxy generally with weak, meanwhile, Avoid delegate conflicts with default inheritance @property (nonatomic, weak) Id<keyboardtooldelegate> tooldelegate;//class method returns an instance object + (ID) keyboardtool;//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 {//the owner can pass Keyboardtool this class//click on the "Next" button to call the owner's next method Nsarr        Ay *array = [[NSBundle mainbundle] loadnibnamed:@ "Keyboardtool" Owner:nil Options:nil]; Returns the initialized Keyboardtool object return array[0];} #pragma mark-click event//Click on Previous button-(void) previousbtnclicked {if ([_tooldelegate respondstoselector: @selector (Keyboardtool : ButtonType:)] {//Tell the agent, click on the Previous button [_tooldelegate keyboardtool:self Buttontype:kkeyboardtoolbuttontypeprevi    OUs];        }}//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]; }}//clicked the Finish button-(void) donebtnclicked {if ([_toolDelegate respondstoselector: @selector (keyboardtool:buttontype:)]) {//Tell the agent, click the Finish button [_tooldelegate keyboard    Tool: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 ()//An array of all provincial names @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 the 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 agent for the Keyboard tool for the current controller, to receive its internal BTN Click events, perceiving btntype self.tool.toolDelegate = self; 1. Set keyboard tools for all text boxesBar is Custom Keyboardtool for (Uitextfield *field in self.view.subviews) {//If 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 saves all text input box controls, followed by [Self.alltextfields AddO        Bject: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 all the 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 Chinese 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 province data nsdictionary *dict = [Nsdictionary Dictionarywithcontentsoffile:[[nsbundl    E Mainbundle] pathforresource:@ "Cities" oftype:@ "plist"];    All arrays with provincial names 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 city array of the province name corresponding to return 2;} Each column corresponds to how many rows-(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 click Finish, 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++;        }//out the corresponding index of the input box, become the first responder, 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 delegate method of Uitextfield! PhilipWith this delegate, when you 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 Endediting:yes]; return YES;} @end

Data Source








Ios_22 Customizing the keyboard toolbar

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.