2010-06-04 48 views
0

我使用的標記字段設置爲文本標誌字段自動跳躍文本視野下一個字段:如何重置輸入字段的鍵盤?

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

然而,在以前的文本字段的情況下是數字,鍵盤保持數字時自動躍升到下一個字段。有沒有辦法將鍵盤設置爲字母模式?

回答

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的:事件呼叫。我試圖使用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