Remove the Finish button from the numeric keypad on a new keyboard boot

Well, I will try to explain it as best as possible. I have an iPhone app and it has a text box that the user can only enter Numbers. No problem. However, there is no button on the numpad button, so I cannot make it disappear. I could make a button that the user presses to cancel the keyboard, but I would rather make a button because the screen is "busy" as it is.

Well, after some research, I came across this . Wow, that's great. However, I also have another text box that requires a default keyboard. And whenever the keyboard appears, regardless of type, it has a Finish button on it.

Not good.

So, I dig a little more. Read the comments, and people mentioned a way to get rid of the Done button, using the “Edit at the initial stage” property to call the “Done” code when necessary. I also got this mostly. If you enter a number field and then release the keyboard and then enter a normal field, the Finish button will not appear.

However, there is one error when the "Finish" button still appears. If you press the “Numeric field” and then touch the “Normal” field, the keyboard will never “disappear”, so even if it changes from a numeric keypad to a regular keyboard, the button is still present. I want to remove buttom from a view, but I'm not sure how to do it. This is the code I have ...

//Done button for numpad
- (void)keyboardWillShow:(NSNotification *)note {
    if (showDoneButton == YES)
    {   


    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    if ([[[UIDevice currentDevice] systemVersion] hasPrefix:@"3"]) {
        [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
    } else {        
        [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
    }
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];

    // locate keyboard view
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard view found; add the custom button to it
        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
            [keyboard addSubview:doneButton];
    }
        showDoneButton = NO; //This tells done button code to run or not
    }
}

- (void)doneButton:(id)sender {
    NSLog(@"Input: %@", playerOneLifeLabel.text);
    NSLog(@"Input: %@", playerTwoLifeLabel.text);
    [playerOneLifeLabel resignFirstResponder];
    [playerTwoLifeLabel resignFirstResponder];
}

NumPadfield "", :

- (IBAction)needNumberPad:(id)sender{
    showDoneButton = YES;
}

done. , ( ) ​​ NO, , . NO.

, numPad , done . , , " ", , . , !

!

+3
2

. doneButton ,

. keyboardWillShow:(NSNotification *)note keyboardWillShow:(NSNotification *)note

if(!showDoneButton ){
      if(doneButton){
[doneButton removeFromSuperview];
doneButton = nil;
      }
      return;
 }

. keyBoardWillHide

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardWillHide:) 
                                             name:UIKeyboardWillHideNotification 
                                           object:nil];

. keyBoardWillHide

- (void)keyboardWillHide:(NSNotification *)note 
{
if(doneButton)
{
    [doneButton removeFromSuperview];
    doneButton = nil;
}
}

.

+1

:

 //This delegate notify when the user done from the textfield
- (void) textFieldDidEndEditing:(UITextField *)textField{
    [yourTextField resignFirstResponder];  
    [doneButton removeFromSuperview]; 
    doneButton = nil;
}

yourTextField , done . doneButton .

0

Source: https://habr.com/ru/post/1726648/


All Articles