读书人

给数字键盘加下Done按键

发布时间: 2012-09-27 11:11:17 作者: rapoo

给数字键盘加上Done按键

默认的数字键盘没有Done按键,如下图:

#import <UIKit/UIKit.h>@interface KeyboardViewController: UIViewController { UITextField *textField;}@end

#import "KeyboardViewController.h"@implementation KeyboardViewController- (void)loadView { self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; self.view.backgroundColor = [UIColor groupTableViewBackgroundColor]; textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 26)]; textField.borderStyle = UITextBorderStyleRoundedRect; textField.keyboardType = UIKeyboardTypeNumberPad; textField.returnKeyType = UIReturnKeyDone; textField.textAlignment = UITextAlignmentLeft; textField.text = @"12345"; [self.view addSubview:textField]; [textField release]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];}- (void)keyboardWillShow:(NSNotification *)note { UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; doneButton.frame = CGRectMake(0, 163, 106, 53); doneButton.adjustsImageWhenHighlighted = NO; [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal]; [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted]; [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside]; UIWindow *tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; UIView *keyboard; for(int i = 0; i < [tempWindow.subviews count]; i++) { keyboard = [tempWindow.subviews objectAtIndex:i];if(([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES) || ([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)) [keyboard addSubview:doneButton]; }}- (void)doneButton:(id)sender { [textField resignFirstResponder];}- (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; [textField release]; [super dealloc];}@end

读书人网 >移动开发

热点推荐