IOS_22 custom keyboard Toolbar

Source: Internet
Author: User

IOS_22 custom keyboard Toolbar

Finally:





Main. Hits? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcnlib2fyzdwvc3ryb25np1_vcd4kpha + pgltzybzcm9" http://www.2cto.com/uploadfile/Collfiles/20140825/2014082508533678.png "alt =" \ ">




KeyboardTool. xib




KeyboardTool. h

// KeyboardTool. h // keyboard processing // Created by beyond on 14-8-24. // Copyright (c) 2014 com. beyond. All rights reserved. # import
  
   
@ Protocol KeyboardToolDelegate; typedef enum {kKeyboardToolButtonTypeNext, // next button, // The previous button kKeyboardToolButtonTypeDone // completion button} KeyboardToolButtonType; @ interface KeyboardTool: UIToolbar // The previous button control @ property (nonatomic, weak) IBOutlet UIBarButtonItem * previusbtn; // The Next button control @ property (nonatomic, weak) IBOutlet UIBarButtonItem * nextBtn; // complete the button control @ property (nonatomic, weak) IBOutlet UIBarButtonItem * doneBtn; // The proxy generally uses weak and avoids conflict with the default inherited delegate @ property (nonatomic, weak) id
   
    
ToolDelegate; // The class method returns an Instance Object + (id) keyboardTool; // listens to the Click Event of the three buttons on the toolbar-(IBAction) previusbtnclicked;-(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 upload the KeyboardTool class. // when you click the "next" button, call the next method of the owner NSArray * array = [[NSBundle mainBundle] loadNibNamed: @ "keyboardTool" owner: nil options: nil]; // return the initialized KeyboardTool object return array [0];} # pragma mark-Click Event // click the previous button-(void) previusbtnclicked {if ([_ toolDelegate respondsToSelector: @ selector (keyboardTool: buttonType :)]) {// tell the proxy that the previous button [_ toolDelegate keyboardTool: self buttonType: kKeyboardToolButtonTypePrevious] is clicked;} // click the next button-(void) nextBtnClicked {if ([_ toolDelegate respondsToSelector: @ selector (keyboardTool: buttonType :)]) {// tell the proxy that the next button [_ toolDelegate keyboardTool: self buttonType: kKeyboardToolButtonTypeNext] ;}// click the finish button-(void) doneBtnClicked {if ([_ toolDelegate respondsToSelector: @ selector (keyboardTool: buttonType :)]) {// tell the proxy, click the finish button [_ toolDelegate keyboardTool: self buttonType: kKeyboardToolButtonTypeDone]; }}@ end


KeyboardToolDelegate. h

/// KeyboardToolDelegate. h // 22_keyboard case /// Created by beyond on 14-8-24. // Copyright (c) 2014 com. beyond. all rights reserved. // # import
  
   
@ Class KeyboardTool; @ protocol KeyboardToolDelegate
   
    
-(Void) keyboardTool :( KeyboardTool *) tool buttonType :( KeyboardToolButtonType) type; @ end
   
  

BeyondViewController. h

//// BeyondViewController. h // 22_keyboard case /// Created by beyond on 14-8-24. // Copyright (c) 2014 com. beyond. all rights reserved. // # import
  
   
# Import "KeyboardTool. h" # import "KeyboardToolDelegate. h" @ interface BeyondViewController: UIViewController
   
    
// 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 case /// Created by beyond on 14-8-24. // Copyright (c) 2014 com. beyond. all rights reserved. // # import "BeyondViewController. h "@ interface BeyondViewController () // All provincial arrays @ property (nonatomic, strong) NSArray * provinceNameArr; // dictionary: an array composed of province name key --- value city names @ property (nonatomic, strong) NSDictionary * provinceName_cities_Dict; // input box of the active State currently activated @ property (nonatomic, weak) UITextField * currentTextField; // toolbar @ property (nonatomic, weak) KeyboardTool * tool on the keyboard; // an array composed of all input box controls @ property (nonatomic, strong) NSMutableArray * allTextFields; @ end @ implementation BeyondViewController-(void) viewDidLoad {[super viewDidLoad]; self. allTextFields = [NSMutableArray array]; // class method, instantiate a KeyboardTool object self. tool = [KeyboardTool keyboardTool]; self. tool. backgroundColor = [UIColor cle ArColor]; self. tool. barTintColor = [UIColor lightGrayColor]; // set the proxy of the keyboard tool to the current controller, which is used to receive btn click events and perceive btnType self. tool. toolDelegate = self; // 1. set the keyboard Toolbar of all text boxes to 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 of each text input box is it... field. inputAccessoryView = self. tool; // array to save all text input box controls, followed by [self. allTextFields addObject: field]; // sets the proxy of each text input box to be the current controller field. delegate = self;} // 2. set the keyboard to DatePicker [self setInputViewForBirthdayTextField]; // 3. set the keyboard to DatePicker [self setInputViewForCityTextField]; // 4. load all data [self loadAllData];} // 2. input for birthday Box, set the keyboard to DatePicker-(void) setInputViewForBirthdayTextField {// set the keyboard for the birthday (no width or height or location required) UIDatePicker * datePicker = [[UIDatePicker alloc] init]; // set the region to Chinese Simplified Chinese datePicker. locale = [[NSLocale alloc] initWithLocaleIdentifier: @ "zh_CN"]; // The mode is: only the date datePicker is displayed. datePickerMode = UIDatePickerModeDate; // listens to the datePicker value change event [datePicker addTarget: self action: @ selector (datePickerValueChangeed :) forControlEvents: UIC OntrolEventValueChanged]; // set it to view self in the birthday input box. birthdayTextField. inputView = datePicker;} // 3. set the keyboard to DatePicker-(void) setInputViewForCityTextField {// set the city keyboard UIPickerView * picker = [[UIPickerView alloc] init]; // set the data source and proxy picker. dataSource = self; picker. delegate = self; // Display Indicator picker. showsSelectionIndicator = YES; // set it to view self in the city input box. cityTextField. inputView = picker;} // 4. load all data -( Void) loadAllData {// load the province data NSDictionary * dict = [NSDictionary dictionaryWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "cities" ofType: @ "plist"]; // All arrays self. provinceNameArr = dict [@ "provinces"]; // dictionary. The key is a province name and the value is an array of city names self. provinceName_cities_Dict = dict [@ "cities"];} // 2.1 listen to the event of changing the value of the birthday Selection control, assign a value to the birthday input box-(void) datePickerValueChangeed :( UIDatePicker *) picker {NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; formatter. dateFormat = @ "yyyy-MM-dd"; // value self in the birthday input box. birthdayTextField. text = [formatter stringFromDate: picker. date] ;}# pragma mark-PickerView data source method // total number of columns-(NSInteger) numberOfComponentsInPickerView :( UIPickerView *) pickerView {// The first column is named by province, the second column is the province-specific city array return 2;} // The number of rows in each column-(NSInteger) pickerView :( UIPickerView *) pickerView numberOfRowsInComponent :( NSInteger) compon Ent {if (component = 0) {// return the return self. provinceNameArr. count;} else {// return the row number NSUInteger rowNum = [pickerView selectedRowInComponent: 0] currently selected in column 1st; // first from the province name array, retrieve the corresponding provincial name NSString * pName = self. provinceNameArr [rowNum]; // obtain the city array by name in the dictionary, and return the length of NSArray * cityArr = self. provinceName_cities_Dict [pName]; return cityArr. count ;}}// what data is displayed per row in each column-(NSString *) pickerView :( UIPickerView *) pickerView TitleForRow :( NSInteger) row forComponent :( NSInteger) component {if (component = 0) {// The previous column shows the province name return self. provinceNameArr [row];} else {// returns the currently selected row number of column 1st NSUInteger rowNum = [pickerView selectedRowInComponent: 0]; // first from the province name array, retrieve the corresponding provincial name NSString * pName = self. provinceNameArr [rowNum]; // obtain the city array by name in the dictionary, and return the length of NSArray * cityArr = self. provinceName_cities_Dict [pName]; return cityArr [row] ;}// UIPickerVi When a row is selected, ew calls-(void) pickerView :( UIPickerView *) pickerView didSelectRow :( NSInteger) row inComponent :( NSInteger) component {// refresh the data in the next column, association result [pickerView reloadComponent: 1]; NSUInteger pRowNum = [pickerView selectedRowInComponent: 0]; // obtain the corresponding provincial name NSString * pName = self from the provincial name array first. provinceNameArr [pRowNum]; NSUInteger cRowNum = [pickerView selectedRowInComponent: 1]; // retrieve the city array by name in the dictionary, and return the corresponding city name NSArray * cit YArr = self. provinceName_cities_Dict [pName]; NSString * cName = cityArr [cRowNum]; // value self in the city input box. cityTextField. text = [NSString stringWithFormat: @ "% @", pName, cName] ;}# pragma mark-key !!!!!!!! KeyboardTool proxy method-(void) keyboardTool :( KeyboardTool *) tool buttonType :( KeyboardToolButtonType) type {if (type = kKeyboardToolButtonTypeDone) {// The input box of the current activity when you click Finish, cancel the first responder and exit the keyboard [self. currentTextField resignFirstResponder];} else {// retrieves the index of the current input box in the input box array, NSUInteger index = [self. allTextFields indexOfObject: self. currentTextField]; if (type = kKeyboardToolButtonTypePrevious) {// when you click the previous one, the index minus 1, index --;} el Se {// when you click Next, the index is added with 1, index ++;} // retrieves the input box of the corresponding index to become the first responder, call up the corresponding keyboard UITextField * field = self. allTextFields [index]; [field becomeFirstResponder] ;}# pragma mark-key point !!!!!!! UITextField proxy method-(void) textFieldDidBeginEditing :( UITextField *) textField {// remember the activated text box. self is used in keyboardTool: buttonClick: In other methods. currentTextField = textField; // obtain the index NSUInteger index = [self. allTextFields indexOfObject: textField]; // you can specify whether the next button is available. tool. nextBtn. enabled = index! = Self. allTextFields. count-1; // you can specify whether self. tool. previusbtn. enabled = index! = 0;} // This Is A delegate method of UITextField! // With This delegate, when we open the keyboard, click return to close the keyboard-(BOOL) textFieldShouldReturn :( UITextField *) textField {// End All edits and exit all keyboards, if YES is returned, [self. view endEditing: YES]; return YES;} @ end

Data Source








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.