【问题标题】:How to reset keyboard for an entry field?如何重置输入字段的键盘?
【发布时间】:2011-02-27 15:42:28
【问题描述】:

我使用标签字段作为文本字段的标志 文本视图字段用于自动跳转到下一个字段:

- (BOOL)findNextEntryFieldAsResponder:(UIControl *)field {
  BOOL retVal = NO;
  for (UIView* aView in mEntryFields) {
    if (aView.tag == (field.tag + 1)) {
      [aView becomeFirstResponder];
      retVal = YES;
      break;
    }
 }
 return retVal;
}

当按下 Next 键时,它可以自动跳转到下一个字段。但是,我的情况是键盘在某些领域是不同的。例如,一个字段是数字和标点符号,下一个是默认字段(字母键)。对于数字和标点符号键盘是可以的,但下一个字段将保持相同的布局。它需要用户按 123 才能返回 ABC 键盘。

我不确定是否有任何方法可以将字段的键盘重置为在 xib 中定义的键盘?不确定是否有可用的 API?我想我必须做点什么是下面的委托?

-(void)textFieldDidBegingEditing:(UITextField*) textField {
  // reset to the keyboard to request specific keyboard view?
  ....
}

好的。我找到了a solution close to my case by slatvik

-(void) textFieldDidBeginEditing:(UITextField*) textField {
  textField.keyboardType = UIKeybardTypeAlphabet;
}

但是,如果前面的文本字段是数字,则键盘在自动跳转到下一个字段时会保持数字。有没有办法将键盘设置为字母模式?

【问题讨论】:

    标签: iphone objective-c


    【解决方案1】:

    我终于找到了解决问题的方法。就我而言,我喜欢使用 Entry 或 Next 键来自动跳转到下一个可用字段。如果顺序的两个字段具有完全不同的键盘,则键盘更改应该没问题。但是,如果一个是带数字模式的键盘,而下一个是字母模式,那么自动跳转不会导致同一个键盘改变模式。

    主要原因是我对 findNextEntryFieldAsResponder: 方法的调用是在 textFieldShouldReturn: 委托方法中完成的。导致下一个字段的调用成为响应者:

     ...
     [aView becomeFirstResponder]; // cause the next fields textFieldDidBeginEditing: event
     ...
    

    我在我的 NSLog 调试消息中发现了这个:

     textFieldShouldReturn: start
       findNextEntryFieldAsResponder
       textFieldDidBeginEditing: start
       ...
       textFieldDidBeginEditing: end
       ...
     textFieldShouldReturn: end
    

    我需要做的是从 textFieldShouldReturn: 事件调用中将下一个字段作为响应者。我尝试使用 iphone 的本地通知框架在 textFieldShouldReturn: 中触发异步通知事件,它符合我的预期。

    这是我更新的代码:

    - (BOOL)findNextEntryFieldAsResponder:(UIControl *)field {
      BOOL retVal = NO;
      for (UIView* aView in mEntryFields) {
        if (aView.tag == (field.tag + 1)) {
          if ([self.specialInputs containsObject:[NSNumber numberWithInt:aView.tag]]) {
            NSNotification* notification = [NSNotification notificationWithName:
                @"myNotification" object:aView];
            [[NSNotificationQueue defaultQueue]
               enqueueNotification:notification postingStyle:NSPostWhenIdle
               coaslesceMask:NSNotificationCoalescingOnName forModes:nil];
            [[NSNotifiationCenter defaultCenter] addObserver:self
               selector:@selector(keyboardShowNofication:)
               name:@"myNotification" object:nil];
          }
          else {
            [aView becomeFirstResponder];
          }
          retVal = YES;
          break;
        }
      }
      return retVal;
    }
    ...
    // Notification event arrives!
    -(void) keyboardShowNofication:(NSNotification*) notification {
      UIResponder* responder = [notification object];
      if (responder) {
         [responder becomeFirstResponder]; // now the next field is responder
      }
    }
    ...
    -(void) dealloc {
      ...
      // remember to remove all the notifications from the center!
      [[NSNotificationCenter defaultCenter] removeObserver:self];
      ...
    }
    

    其中 specialInputs 是一个 int 值的 NSArray。它是一个可以使用列表标签设置为特殊输入的属性。实际上,我认为所有的输入都可以被视为 specialInputs 并且它也可以正常工作(只是更多的通知)。

    我有a complete description of codes in my blog

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 2016-08-31
      • 2016-09-14
      • 2019-11-15
      相关资源
      最近更新 更多