首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

给数字键盘加下Done按键

2012-09-27 
给数字键盘加上Done按键默认的数字键盘没有Done按键,如下图:#import UIKit/UIKit.h@interface KeyboardV

给数字键盘加上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

热点排行