Disapperiang IPhone数字键盘

时间:2009-09-10 22:46:16

标签: iphone objective-c keypad

如何在iPhone中消失数字键盘,因为其中没有返回键,这意味着它不会响应

- (BOOL)textFieldShouldReturn:(UITextField *)textField

5 个答案:

答案 0 :(得分:3)

在touchesBegan中使用resignFirstResponder,如下所示:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    if(touch.phase == UITouchPhaseBegan) {
        [self.myTextField resignFirstResponder];
    }
}

如果您不确定焦点当前位于何处,您可能需要在多个文本字段上调用此字段;我没有测试那个案例。

此外,在Interface Builder中,您需要为视图启用“启用用户交互”复选框,或以编程方式使用:

myView.userInteractionEnabled = YES;

答案 1 :(得分:2)

这是答案..

-(void) touchesBegan :(NSSet *) touches withEvent:(UIEvent *)event

{

    //[URL resignFirstResponder];

    //[Password resignFirstResponder];

    [speedField resignFirstResponder];

    [super touchesBegan:touches withEvent:event ];

}

答案 2 :(得分:0)

试试这个:

[theTextField resignFirstResponder];

答案 3 :(得分:0)

为什么不使用“数字和标点符号”键盘而不是“数字”?它会节省你的时间

答案 4 :(得分:-2)

你走了。用这个扩展你的UIViewController类。但我们怎么知道它是完成按钮? Adding Done Button to Only Number Pad Keyboard on iPhone

//
//  UIViewController+NumPadReturn.m
//  iGenerateRandomNumbers
//
//  Created by  on 12/4/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "UIViewController+NumPadReturn.h"


@implementation UIViewController (NumPadReturn)

-(void) viewDidLoad{
    // add observer for the respective notifications (depending on the os version)
    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)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)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)doneButton:(id)sender {
    NSLog(@"doneButton");
    [self.view endEditing:TRUE];
}