2009-06-14 69 views
27

我使用「返回鍵」的「下一步」值來獲取下一步按鈕來代替完成按鈕,但是(顯然)按下它不會自動移動到我的視圖中的下一個UITextField。使用UITextField上「返回」按鈕的「下一步」版本移動到下一個UITextField的最佳方法

什麼是正確的方法來做到這一點?在一個更大的主題上,有哪些關於在iPhone SDK中正確構建表單的技巧?

+1

爲了紀念一個答案是正確的,只要按一下旁邊的空複選標記圖標答案... .. – 2012-04-02 13:29:59

回答

36

使某個對象成爲第一個文本字段的委託,並實現- (BOOL)textFieldShouldReturn:(UITextField *)textField方法;在那裏,請撥打第二個文本字段的-becomeFirstResponder。從這將返回YES將使文本字段執行其返回按鈕的默認行爲 - 我認爲這通常是發送其操作消息。如果你沒有添加任何東西作爲該動作的目標,那麼返回的內容並不重要。

+1

。如果你在最後一個UITextField中,你應該調用resignFirstResponder來隱藏鍵盤。 – 2009-11-09 20:46:18

+0

這是一個完全不同的方式比這裏解釋(http://stackoverflow.com/questions/467881/how-do-you-change-the-uicontrol-with-focus-on-iphone)或將都工作以及? – HollerTrain 2010-01-03 00:40:00

+0

兩者都可以工作。我的回答使用委託制度,布拉德的目標行動的東西。 – 2010-01-03 06:20:46

34

要建立在諾亞回答,如果你有很多文本框的,不覺得像有一堆如果的,你能做到這樣的:當你標記每一個文本框始於

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    //[[self.view viewWithTag:textField.tag+1] becomeFirstResponder]; 

    UIView *view = [self.view viewWithTag:textField.tag + 1];  
    if (!view)   
      [textField resignFirstResponder];  
    else   
      [view becomeFirstResponder]; 
    return YES; 
} 

任何數字,只要他們順序標記,在故事板或代碼中,它應該工作。

1

這似乎工作得很好,並不需要許多建議的標籤系統。雖然有這個解決方案有兩點需要注意:

  • 所有的UITextFields必須在同一個UIView(具有相同的超級視圖)。
  • UITextFields需要在界面生成器中按照正確的順序排列。

    -(BOOL)textFieldShouldReturn:(UITextField *)textField { 
        /* 
        * 1. Loop through the textfield's superview 
        * 2. Get the next textfield in the superview 
        * 3. Focus that textfield 
        */ 
    
        UIView *superView = [textField superview]; 
        BOOL foundCurrent = false; 
        for (UITextField *tf in superView.subviews) { 
         // Set focus on the next textfield 
         if (foundCurrent) { 
         [tf becomeFirstResponder]; 
         return NO; 
         } 
    
        //Find current textfield 
        if ([tf isEqual:textField]) { 
         foundCurrent = true; 
         } 
        } 
    
        return YES; 
    }  
    
0

我一直在努力與這個問題太...,因此我創建小型圖書館處理多個文本框。你可以在github上找到它GNKeyboardAwareScrollView#GNTextFieldsManager

NSArray *myTextFields = @[...]; // the order of array matters! 
GNTextFieldsManager *manager = [[GNTextFieldsManager alloc] initWithTextFields:myTextFields]; 

,或者通過指定父視圖(並設置所有視圖標籤):

GNTextFieldsManager *manager = [[GNTextFieldsManager alloc] initWithView:self.view]; 

希望我會爲別人有用

你可以用文本框的數組初始化它: )

1

我不喜歡處理標籤,所以這裏是我的解決方案。在你的ViewController中創建一個IBOutletCollection的所有文本字段,拖動以從上到下依次連接你的文本字段。

@interface ViewController() <UITextFieldDelegate> 
@property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *allTextFields; 
@end 

在viewDidLoad中設置您的textFields委託。 (或將其設置在故事板中)。

for (VVTextField *tf in self.allTextFields) { 
    tf.delegate = self; 
} 

然後實現的UITextField代表

#pragma mark - UITextField Delegate 

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    NSUInteger currentIndex = [self.allTextFields indexOfObject:textField]; 
    NSUInteger nextIndex = currentIndex+1; 
    if (nextIndex < self.allTextFields.count) { 
     [[self.allTextFields objectAtIndex:nextIndex] becomeFirstResponder]; 
    } else { 
     [[self.allTextFields objectAtIndex:currentIndex] resignFirstResponder]; 
    } 
    return YES; 
} 
2

對於斯威夫特:

func textFieldShouldReturn(textField: UITextField) -> Bool { 
//your collection of textfields 
    guard let i = textFields.indexOf(textField) else { return false } 
    if i + 1 < textFields.count { 
     textFields[i + 1].becomeFirstResponder() 
     return true 
    } 
    textField.resignFirstResponder() 
    return true 
} 
相關問題