views:

458

answers:

1

Alright, I'll try to explain this the best I can. I have an iPhone app and it has a text field which the user can only input Numbers. Not a problem there. However, there is no done button on the numpad, so I can't make it disappear. I could make a button that the user presses to dismiss the keyboard, but I'd rather have a done button because the screen is "busy" as is.

Well, after some research, I came across this. Wow, that worked great. However, I also have another text field which requires the default keyboard. And whenever the keyboard comes up, regardless of the type, it has the "Done" button on it.

Not good.

So I do some more digging. Read through the comments, and people mentioned a way to get rid of the Done button using the "Editing Did Begin" trait, to only call the "Done Button" code when necessary. I also got that mostly done. If you type in the number field, and then dismiss the keyboard, and then type in the normal field, the done button does not appear.

However, there is one bug, where the "Done" button still appears. If you tap the Numeric Field, and then tap the Normal field, the keyboard never "disappears", so even though it changes from a numpad to a normal keyboard, the button still is there. I want to remove the buttom from the view, but I'm not sure how to go about this. 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];
}

When the NumPadfield triggers "editing did begin" it calls this function:

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

Which then makes sure the done button is shown. When the done button code is done, the variable (as seen above) is set back to NO, so it doesn't show up again if you tap the default text field. The Boolean variable is set to NO as default.

What needs to happen is if you jump immediately from editing the numPad field to the default field, the done button disappears. I can put the code in the function that is called when "Editing did end" but I don't know what to put there. Help would be appreciated, I've gotten this far!

Thank you!

A: 

Follow these steps

a. First make the doneButton an instance varible of your class, this will help u maintain the reference to the button

b. Add this code at the beginning of ur keyboardWillShow:(NSNotification *)note method

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

c. add notification for keyBoardWillHide

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

d. in keyBoardWillHide method do the following

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

This should do the trick

RVN