如何解雇iOS Decimal键盘?

时间:2014-08-16 20:24:48

标签: ios objective-c iphone xcode keyboard

我正在创建一个iOS应用程序,要求用户为多个文本字段输入数值。在UITextField属性下,我使用了

键盘 =十进制键

返回键 =完成

键盘占用空间并隐藏"提交表格"按钮。

UIViewController.h 文件下,我为每个UITextField编写了以下代码,事件为在退出时结束

-(IBAction)dismissKeyboard:(id)sender;

UIViewController.m 文件下,我为每个UITextField编写了以下代码:

-(IBAction)dismissKeyboard:(id)sender {

    [textField becomeFirstResponder];
    [textField resignFirstResponder];
}

当我为Xcode 5打开iOS模拟器时,我在屏幕键盘上没有任何 DONE 字段。但是,如果我从MacBook键盘按RETURN键,屏幕键盘就会消失。

我怎么能 -

  • 在iOS键盘上显示DONE键?
  • 取消键盘?

5 个答案:

答案 0 :(得分:8)

由于建议滚动视图,我将推荐另一种方法。

对于每个数字输入字段,设置输入附件视图 - 工具栏 - 其中包含“完成”按钮。它看起来像Safari中键盘上方的条形图。用户点击此完成按钮后,像往常一样关闭键盘。

这是最简单的解决方案。

UIToolbar* toolbar = [UIToolbar new];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissKeyboard:)];
id space = [UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
toolbar.items = @[space, doneButton];

self.numericTextField1.inputAccessoryView = toolbar;
self.numericTextField2.inputAccessoryView = toolbar;
...

答案 1 :(得分:2)

在.h类中,首先声明一个滚动视图。

@property (strong, nonatomic) IBOutlet UIScrollView *scrRegistration;

在.m类中,使用这些代码

-(void)viewWillAppear:(BOOL)animated 
{
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard:)];
    [gesture setNumberOfTapsRequired:1];
    [gesture setNumberOfTouchesRequired:1];
    [self.scrRegistration addGestureRecognizer:gesture];
    [self.scrRegistration setContentSize:CGSizeMake(320, 354)];
    for (UIView *vw in [self.scrRegistration subviews]) {
        if([vw isKindOfClass:[UITextField class]]){
            UITextField *txtfld=(UITextField *)vw;
            txtfld.attributedPlaceholder = [[NSAttributedString alloc] initWithString:txtfld.placeholder attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:8.0f/255.0f green:94.0f/255.0f blue:165.0f/255.0f alpha:1.0]}];
        }
    }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if(textField==DecimalKeyboard) {
        if([[UIScreen mainScreen] bounds].size.height>480) {
            [self.scrRegistration setContentOffset:CGPointMake(0, 110) animated:YES];
        } else {
            [self.scrRegistration setContentOffset:CGPointMake(0, 300) animated:YES];
        }
        UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
        numberToolbar.barStyle = UIBarStyleBlackTranslucent;
        numberToolbar.items = [NSArray arrayWithObjects:
                               [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
                               [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                               [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                               nil];
        [numberToolbar sizeToFit];
        textField.inputAccessoryView = numberToolbar;
   }
}




-(void)hideKeyboard:(UITapGestureRecognizer *)tap 
{
    [DecimalKeyboard resignFirstResponder];
    [self.scrRegistration setContentOffset:CGPointMake(0, 0) animated:YES];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.35];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView commitAnimations];
}



-(void)cancelNumberPad
{
    for (UIView *vw in [self.scrRegistration subviews]) {
        if([vw isKindOfClass:[UITextField class]]) {
            UITextField *txtfld=(UITextField *)vw;
            if ([txtfld isFirstResponder] == YES) {
                [txtfld resignFirstResponder];
            }
        }
    }

    [self.scrRegistration setContentOffset:CGPointMake(0, 0) animated:YES];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.35];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView commitAnimations];
}



-(void)doneWithNumberPad
{
    [DecimalKeyboard resignFirstResponder];
    [self.scrRegistration setContentOffset:CGPointMake(0, 0) animated:YES];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.35];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView commitAnimations];
}

答案 2 :(得分:1)

您可能最好只是将屏幕上的内容放入滚动视图,然后在键盘存在时调整视图大小。这通常是我在输入表单时采用的方法,并且它非常容易实现。为此,您可以按照以下答案中的说明进行操作:

https://stackoverflow.com/a/16044603/3708242

首先,确保为键盘通知注册viewController(并在视图消失时删除观察者):

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

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

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}

这是代码的主要部分。它假设您有一个占据整个屏幕的视图。如果情况并非如此,您需要通过以下两种方法调整CGRect:。

- (void)keyboardWillShow:(NSNotification *)note {
    NSDictionary *userInfo = note.userInfo;
    NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];

    CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil];

    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
        self.contentView.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y);
    } completion:nil];
}

- (void)keyboardWillHide:(NSNotification *)note {
    NSDictionary *userInfo = note.userInfo;
    NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];

    CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil];

    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
        self.contentView.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y);
    } completion:nil];
}

答案 3 :(得分:1)

如果其他人在学习Swift时遇到这个问题,经过很多失败的尝试后,我终于在我的ViewController中使用此代码取得了一些成功,基于Leo Natan的答案:

@IBOutlet var decimalField : UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    // ...

    addDoneButtonToKeyboard()

    // ...

    // Do any additional setup after loading the view, typically from a nib.
    refreshUI()
}

func addDoneButtonToKeyboard() {
    var doneButton:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "hideKeyboard")

    var space:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)

    var items = [AnyObject]()
    items.append(space)
    items.append(doneButton)

    toolbar.items = items

    decimalField.inputAccessoryView = toolbar
}

func hideKeyboard() {
    decimalField.resignFirstResponder()
}

答案 4 :(得分:0)

您应该考虑在滚动视图中输入输入表单。这样,应用程序可以滚动表单,以便提交按钮变得可见。您的用户也可以向上和向下滚动以查看所有输入。

要使用带有完成按钮的数字键盘,您需要创建自定义输入视图。

另一种方法是在键盘外的任何水龙头和文本字段上关闭键盘,但用户体验会很尴尬。