IOS 8.3 Number Pad自定义OK按钮

IOS 8.3 Number Pad custom OK button

我正在开发 IOS 应用程序。我正在为 iPhone 数字键盘使用自定义确定按钮。此代码适用于所有 IOS 版本,但 IOS 8.3 未将 working.OK 按钮添加到键盘,但单击事件不起作用。

- (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];
                }
            }
        }
    }
}

好的,我正在解决这个问题所有 IOS 版本也 IOS 8.3 FYI

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

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