Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
934 views
in Technique[技术] by (71.8m points)

iphone - done button only for numberpad keyboard

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 keyboard. But on numberpad keyboard there is no return key. So I have added "Done"button programmatically by using following code.

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

   - (void)addButtonToKeyboard {
// create custom button
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 163, 106, 53);
doneButton.adjustsImageWhenHighlighted = NO;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
    [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 found, add the button
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
        if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
                               [keyboard addSubview:doneButton];                
    } else {
        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
                   [keyboard addSubview:doneButton];
        }
     }
    }

     - (void)keyboardWillShow:(NSNotification *)note {
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) {

        [self addButtonToKeyboard];


}
  }

       - (void)keyboardDidShow:(NSNotification *)note {
// if clause is just an additional precaution, you could also dismiss it
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {

        [self addButtonToKeyboard];


      }
   }


- (void)doneButton:(id)sender {
    NSLog(@"doneButton");
    NSLog(@"Input: %@", contactno.text);

    [contactno resignFirstResponder];

     }

It is fine for this particular textfield. But this button is also get add for the rest of the textfields. How could unhide this button for other textfields. enter image description here

thanks alot in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...