如何重置输入字段的键盘?

时间:2010-06-04 06:27:25

标签: iphone objective-c

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

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

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

1 个答案:

答案 0 :(得分:0)

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

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

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

我在NSLog调试消息中找到了这个:

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

我需要做的是将下一个字段作为textFieldShouldReturn:event call中的响应者。我尝试使用iphone的本地通知框架在textFieldShouldReturn中触发async-notification事件:它完成了我的期望。

以下是我更新的代码:

- (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

相关问题