done button only for numberpad keyboard

后端 未结 3 1744
生来不讨喜
生来不讨喜 2020-12-18 14:44

I am working on the iPhone application. I have four textfields in one of the view. In these four textfield I have one textfield in which there will be use of numberpad keybo

相关标签:
3条回答
  • 2020-12-18 15:28

    Put this code in your project and just set iboutlet of text field.It have some unused methods too but you can get your choice of done button using that.Then you will remove unused methods.This is just for Get Done button on number pad keyboard

    .H file

    @interface aaaViewController : UIViewController
    {
    IBOutlet UITextField *txthello;
    UIImage *numberPadDoneImageNormal;
    UIImage *numberPadDoneImageHighlighted;
    UIButton *numberPadDoneButton;
    
    }
    
    @property (nonatomic, retain) UIImage *numberPadDoneImageNormal;
    @property (nonatomic, retain) UIImage *numberPadDoneImageHighlighted;
    @property (nonatomic, retain) UIButton *numberPadDoneButton;
    

    .M File

    - (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // Add listener for keyboard display events
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(keyboardDidShow:) 
                                                     name:UIKeyboardDidShowNotification 
                                                   object:nil];     
    } else {
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(keyboardWillShow:) 
                                                     name:UIKeyboardWillShowNotification 
                                                   object:nil];
    }
    
    // Add listener for all text fields starting to be edited
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(textFieldDidBeginEditing:)
                                                 name:UITextFieldTextDidBeginEditingNotification 
                                               object:nil];
    }
    
    - (void)viewWillDisappear:(BOOL)animated {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
        [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                        name:UIKeyboardDidShowNotification 
                                                      object:nil];      
    } else {
        [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                        name:UIKeyboardWillShowNotification 
                                                      object:nil];
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:UITextFieldTextDidBeginEditingNotification 
                                                  object:nil];
    [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)updateKeyboardButtonFor:(UITextField *)textField {
    
    // Remove any previous button
    [self.numberPadDoneButton removeFromSuperview];
    self.numberPadDoneButton = nil;
    
    // Does the text field use a number pad?
    if (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 setTitle:@"Return" forState:UIControlStateNormal];
    //  [self.numberPadDoneButton setFont:[UIFont boldSystemFontOfSize:18]];
    [self.numberPadDoneButton setTitleColor:[UIColor colorWithRed:77.0f/255.0f green:84.0f/255.0f blue:98.0f/255.0f alpha:1.0] forState:UIControlStateNormal];  
    
    [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;
        }
    }
    }
    
    - (void)textFieldDidBeginEditing:(NSNotification *)note {
    [self updateKeyboardButtonFor:[note object]];
    }
    
    - (void)keyboardWillShow:(NSNotification *)note {
    [self updateKeyboardButtonFor:[self findFirstResponderTextField]];
    }
    
    - (void)keyboardDidShow:(NSNotification *)note {
    [self updateKeyboardButtonFor:[self findFirstResponderTextField]];
    }
    
    - (IBAction)numberPadDoneButton:(id)sender {
    UITextField *textField = [self findFirstResponderTextField];
    [textField resignFirstResponder];
    }
    
    
    - (void)dealloc {
    [numberPadDoneImageNormal release];
    [numberPadDoneImageHighlighted release];
    [numberPadDoneButton release];
    [super dealloc];
    }
    
    0 讨论(0)
  • 2020-12-18 15:37

    Please please please please please please please please please please please please please do not do this. This is so ridiculously fragile. Consider this:

    • what if the layout of the number keypad changes?
    • what if the colors of the keys change?
    • what if the implementation of the keyboard changes such that it's no longer the window at index 1?
    • what if the implementation of the keyboard and peripheral host change such that introspecting the description breaks?
    • what if you're running this on iPad where the keyboard has a totally different layout?

    These are just a couple of the myriad of problems that come with glomming your UI into private view hierarchies.

    DON'T DO THIS.

    Here are 2 alternatives instead:

    1. Use the -inputAccessoryView property on every UIResponder (and thus every UIView) to define a UIToolbar with a "Done" button. The inputAccessoryView will be positioned above the keyboard as the keyboard animates in and out.

    2. Add a transparent UIView over your entire UI that captures tap events and causes the keyboard to dismiss

    0 讨论(0)
  • 2020-12-18 15:37

    I suppose you could keep a global variable UITextField * selectedTextField as a pointer (weak reference) to the textField the user currently has selected. Also store IBOutlets to all the UITextField object on your view (strong references). Then wrap the entire addButtonToKeyboard body in a big if clause:

    if (self.selectedTextField == self.contactNumberTextField){
        // ... add button
    }
    

    You'll need to set up the UITextField delegate method

    - (void)textFieldDidBeginEditing:(UITextField *)textField;
    

    to know which textField is the currently selected one.

    In fact I think you could even do it without the outlets, using selectedTextField.keyboardType property, but then again, you'll probably already have the outlets anyway since you need them to read user input from them.

    Hope this helps!

    0 讨论(0)
提交回复
热议问题