IOS 8.3数字键盘自定义OK按钮
我正在开发一个IOS应用程序。我正在使用iPhone数字键盘的自定义Ok按钮。此代码适用于所有IOS版本,但IOS 8.3不工作。键盘上添加了“确定”按钮,但单击“事件不工作”IOS 8.3数字键盘自定义OK按钮,ios,objective-c,ios8.3,Ios,Objective C,Ios8.3,我正在开发一个IOS应用程序。我正在使用iPhone数字键盘的自定义Ok按钮。此代码适用于所有IOS版本,但IOS 8.3不工作。键盘上添加了“确定”按钮,但单击“事件不工作” - (void)updateKeyboardButtonFor:(UITextField *)textField { // Remove any previous button [self.numberPadDoneButton removeFromSuperview]; self.number
- (void)updateKeyboardButtonFor:(UITextField *)textField {
// Remove any previous button
[self.numberPadDoneButton removeFromSuperview];
self.numberPadDoneButton = nil;
// Does the text field use a number pad?
if (textField==nil || textField.keyboardType != UIKeyboardTypeNumberPad){
return;
}
// If there's no keyboard yet, don't do anything
if ([[[UIApplication sharedApplication] windows] count] < 2)
return;
UIWindow *keyboardWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
// Create new custom button
self.numberPadDoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.numberPadDoneButton.frame = CGRectMake(0, 163, 106, 53);
self.numberPadDoneButton.adjustsImageWhenHighlighted = FALSE;
[self.numberPadDoneButton setImage:self.numberPadDoneImageNormal forState:UIControlStateNormal];
[self.numberPadDoneButton setImage:self.numberPadDoneImageHighlighted forState:UIControlStateHighlighted];
[self.numberPadDoneButton addTarget:self action:@selector(numberPadDoneButton:) forControlEvents:UIControlEventTouchUpInside];
/*
// Locate keyboard view and add button
NSString *keyboardPrefix = [[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2 ? @"<UIPeripheralHost" : @"<UIKeyboard";
for (UIView *subView in keyboardWindow.subviews) {
if ([[subView description] hasPrefix:keyboardPrefix]) {
[subView addSubview:self.numberPadDoneButton];
[self.numberPadDoneButton addTarget:self action:@selector(numberPadDoneButton:) forControlEvents:UIControlEventTouchUpInside];
break;
}
}
*/
UIWindow* tempWindow;
UIView* keyboard;
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
if ([NSStringFromClass([window class]) isEqualToString:@"UITextEffectsWindow"])
{
tempWindow = window;
break;
}
}
for(int i = 0 ; i < [tempWindow.subviews count] ; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
// keyboard found, add the button
if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES){
[keyboard addSubview:self.numberPadDoneButton];
}
//This code will work on iOS 8.0
else if([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] == YES){
for(int i = 0 ; i < [keyboard.subviews count] ; i++)
{
UIView* hostkeyboard = [keyboard.subviews objectAtIndex:i];
if([[hostkeyboard description] hasPrefix:@"<UIInputSetHost"] == YES){
[hostkeyboard addSubview:self.numberPadDoneButton];
BOOL isPortrait = UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
self.numberPadDoneButton.frame = CGRectMake(((isPortrait) ? 0 : -1),((int) (hostkeyboard.frame.size.height*3)/4) + ((isPortrait) ? 0 : 1),(int) hostkeyboard.frame.size.width/3-1, (isPortrait) ? 60 : 40);
[self.numberPadDoneButton addTarget:self action:@selector(numberPadDoneButton:) forControlEvents:UIControlEventTouchUpInside];
}
}
}
}
}
-(void)updateKeyboardButton:(UITextField*)textField{
//删除以前的任何按钮
[self.numberPadDoneButton从SuperView中移除];
self.numberPadDoneButton=nil;
//文本字段是否使用数字键盘?
if(textField==nil | | textField.keyboardType!=UIKeyboardTypeNumberPad){
返回;
}
//如果还没有键盘,什么都不要做
如果([[[UIApplication sharedApplication]windows]计数]<2)
返回;
UIWindow*键盘窗口=[[[UIApplication sharedApplication]windows]对象索引:1];
//创建新的自定义按钮
self.numberPadDoneButton=[UIButton Button类型:UIButtonTypeCustom];
self.numberPadDoneButton.frame=CGRectMake(0,163,106,53);
self.numberPadDoneButton.adjustsImageWhenHighlighted=FALSE;
[self.numberPadDoneButton设置图像:self.numberPadDoneImageNormal for状态:UIControlStateNormal];
[self.numberPadDoneButton设置图像:self.numberPadDoneImageHighlighted for状态:UIControlStateHighlighted];
[self.numberPadDoneButton addTarget:self action:@selector(numberPadDoneButton:)for controlEvents:UIControlEventTouchUpInside];
/*
//找到“键盘视图”和“添加”按钮
NSString*keyboardPrefix=[[[UIDevice currentDevice]systemVersion]floatValue]>=3.2?@“好的,我正在解决此问题所有IOS版本也包括IOS 8.3供参考
NumpadViewController.h
#import <UIKit/UIKit.h>
@interface NumberPadViewController : BaseViewController {
UIImage *numberPadDoneImageNormal;
UIImage *numberPadDoneImageHighlighted;
UIButton *numberPadDoneButton;
}
@property (nonatomic, retain) UIImage *numberPadDoneImageNormal;
@property (nonatomic, retain) UIImage *numberPadDoneImageHighlighted;
@property (nonatomic, retain) UIButton *numberPadDoneButton;
- (IBAction)numberPadDoneButton:(id)sender;
- (void)doneButtonClick:(id)sender;
@end
#导入
@接口号AdviewController:BaseViewController{
UIImage*numberPadDoneImageNormal;
UIImage*numberPadDoneImageHighlighted;
UIButton*numberPadDoneButton;
}
@属性(非原子,保留)UIImage*numberPadDoneImageNormal;
@属性(非原子,保留)UIImage*numberPadDoneImageHighlighted;
@属性(非原子,保留)UIButton*numberPadDoneButton;
-(iAction)numberPadDoneButton:(id)发送方;
-(void)doneButtonClick:(id)发送方;
@结束
NumpadViewController.m
#import "NumpadViewController.h"
@implementation NumberPadViewController
@synthesize numberPadDoneImageNormal;
@synthesize numberPadDoneImageHighlighted;
@synthesize numberPadDoneButton;
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
if ((self = [super initWithNibName:nibName bundle:nibBundle]) == nil) {
return nil;
}
if ([Helper isIOS7]) {
self.numberPadDoneImageNormal = [UIImage imageNamed:@"DoneDown7.png"];
self.numberPadDoneImageHighlighted = [UIImage imageNamed:@"DoneUp7.png"];
}else{
self.numberPadDoneImageNormal = [UIImage imageNamed:@"DoneUp.png"];
self.numberPadDoneImageHighlighted = [UIImage imageNamed:@"DoneDown.png"];
}
return self;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[SVProgressHUD dismiss];
if([Helper isDeviceiPhone]){
// Add listener for keyboard display events
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardDidHideNotification
object:nil];
} else {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
// Add listener for all text fields starting to be edited
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textFieldDidBeginEditingHandler:)
name:UITextFieldTextDidBeginEditingNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textFieldDidEndEditingHandler:)
name:UITextFieldTextDidEndEditingNotification
object:nil];
}
}
- (void)viewWillDisappear:(BOOL)animated {
if([Helper isDeviceiPhone]){
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardDidHideNotification
object:nil];
} else {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UITextFieldTextDidBeginEditingNotification
object:nil];
}
if (self.numberPadDoneButton!=nil) {
[self.numberPadDoneButton removeFromSuperview];
}
[super viewWillDisappear:animated];
}
- (UIView *)findFirstResponderUnder:(UIView *)root {
if (root.isFirstResponder)
return root;
for (UIView *subView in root.subviews) {
UIView *firstResponder = [self findFirstResponderUnder:subView];
if (firstResponder != nil)
return firstResponder;
}
return nil;
}
- (UITextField *)findFirstResponderTextField {
UIResponder *firstResponder = [self findFirstResponderUnder:[self.view window]];
if (![firstResponder isKindOfClass:[UITextField class]])
return nil;
return (UITextField *)firstResponder;
}
- (void)textFieldDidBeginEditingHandler:(NSNotification *)note {
[self addButtonToKeyboard:[note object]];
}
- (void)textFieldDidEndEditingHandler:(NSNotification *)note {
[self removedSearchButtonFromKeypad];
}
- (void)keyboardWillShow:(NSNotification *)note {
[self addButtonToKeyboard:[self findFirstResponderTextField]];
}
- (void)keyboardDidShow:(NSNotification *)note {
//[self updateKeyboardButtonFor:[self findFirstResponderTextField]];
[self addButtonToKeyboard:[self findFirstResponderTextField]];
}
- (void)keyboardWillHide:(NSNotification*)note {
[self removedSearchButtonFromKeypad];
}
- (IBAction)numberPadDoneButton:(id)sender {
UITextField *textField = [self findFirstResponderTextField];
[textField resignFirstResponder];
if ( [self respondsToSelector:@selector(doneButtonClick:)] ) {
[self doneButtonClick:textField];
}
}
- (void)doneButtonClick:(id)sender{
}
- (void)dealloc {
[numberPadDoneImageNormal release];
[numberPadDoneImageHighlighted release];
[numberPadDoneButton release];
[super dealloc];
}
- (void)addButtonToKeyboard:(UITextField *)textField
{
[self removedSearchButtonFromKeypad];
if (textField==nil || textField.keyboardType != UIKeyboardTypeNumberPad){
return;
}
// create custom button
self.numberPadDoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
//self.numberPadDoneButton.frame = CGRectMake(0, 163+44, 106, 53);
self.self.numberPadDoneButton.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 53, [[UIScreen mainScreen] bounds].size.width / 3, 53);
self.numberPadDoneButton.adjustsImageWhenHighlighted = NO;
[self.numberPadDoneButton setTag:67123];
[self.numberPadDoneButton setImage:self.numberPadDoneImageNormal forState:UIControlStateNormal];
[self.numberPadDoneButton setImage:self.numberPadDoneImageHighlighted forState:UIControlStateHighlighted];
[self.numberPadDoneButton addTarget:self action:@selector(numberPadDoneButton:) forControlEvents:UIControlEventTouchUpInside];
// locate keyboard view
NSInteger windowCount = [[[UIApplication sharedApplication] windows] count];
if (windowCount < 2) {
return;
}
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIButton* donebtn = (UIButton*)[tempWindow viewWithTag:67123];
if (donebtn == nil)//to avoid adding again and again as per my requirement (previous and next button on keyboard)
[tempWindow addSubview:self.numberPadDoneButton];
}
-(void) removedSearchButtonFromKeypad{
NSInteger windowCount = [[[UIApplication sharedApplication] windows] count];
if (windowCount < 2) {
return;
}
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
for(int i = 0 ; i < [tempWindow.subviews count] ; i++)
{
UIView* keyboard = [tempWindow.subviews objectAtIndex:i];
[self removeButton:keyboard];
}
}
-(void) removeButton:(UIView*)keypadView{
UIButton* donebtn = (UIButton*)[keypadView viewWithTag:67123];
if(donebtn){
[donebtn removeFromSuperview];
donebtn = nil;
}
}
@end
#导入“NumpadViewController.h”
@实现编号AdviewController
@合成数字addoneimagenormal;
@合成numberPadDoneImageHighlighted;
@合成numberPadDoneButton;
-(id)initWithNibName:(NSString*)nibName绑定:(NSBundle*)nibBundle{
if((self=[super initWithNibName:nibName bundle:nibBundle])==nil){
返回零;
}
如果([Helper isIOS7]){
self.numberPadDoneImageNormal=[UIImage ImageName:@“DoneDown7.png”];
self.numberPadDoneImageHighlighted=[UIImage ImageName:@“DoneUp7.png”];
}否则{
self.numberPadDoneImageNormal=[UIImage ImageName:@“DoneUp.png”];
self.numberPadDoneImageHighlighted=[UIImage ImageName:@“DoneDown.png”];
}
回归自我;
}
-(无效)视图将显示:(BOOL)动画{
[超级视图将显示:动画];
[SVD解散];
如果([Helper IsDevicePhone]){
//添加键盘显示事件的侦听器
如果([[[UIDevice currentDevice]systemVersion]floatValue]>=3.2){
[[NSNotificationCenter defaultCenter]添加观察者:self
选择器:@选择器(键盘显示:)
名称:UIKeyboardDidShowNotification
对象:无];
[[NSNotificationCenter defaultCenter]添加观察者:self
选择器:@selector(keyboardWillHide:)
名称:UIKeyboardDidHideNotification
对象:无];
}否则{
[[NSNotificationCenter defaultCenter]添加观察者:self
选择器:@选择器(键盘将显示:)
名称:UIKeyboardWillShowNotification
对象:无];
[[NSNotificationCenter defaultCenter]添加观察者:自选择器:@selector(keyboardWillHide:)名称:UIKeyboardWillHideNotification对象:nil];
}
//为开始编辑的所有文本字段添加侦听器
[[NSNotificationCenter defaultCenter]添加观察者:self
选择器:@selector(textfielddBegineditingHandler:)
名称:UITextFieldTextDidBeginEditingNotification
对象:无];
[[NSNotificationCenter defaultCenter]添加观察者:self
选择器:@selector(textfielddendeditinghandler:)
名称:UITextFieldTextDiEndeditingNotification
对象:无];
}
}
-(无效)视图将消失:(BOOL)已设置动画{
如果([Helper IsDevicePhone]){
如果([[[UIDevice currentDevice]systemVersion]floatValue]>=3.2){
[[NSNotificationCenter defaultCenter]移除观察者:self
名称:UIKeyboardDidShowNotification
对象:无];
[[NSNotificationCenter defaultCenter]移除观察者:self
名称:UIKeyboardDidHideNotification
对象:无];
}否则{
[[NSNotificationCenter defaultCenter]移除观察者:self
名称:UIKeyboardWillShowNotification
对象:无];
[[NSNotificationCenter defaultCenter]移除观察者:self