2012-01-31 176 views
1

我已經在鍵盤上爲上一個/下一個/完成按鈕選擇的文本框創建了一個欄。這樣做時,我注意到我用於在文本字段之間導航的標籤發生了一個奇怪的事件。我正在用循環編程創建我的接口,因此,只需將標記值設置爲循環變量i即可。標籤標籤0的奇怪問題?

我在0開始i變量,所以創建的第一個文本字段的標籤爲零。基本上所發生的是'previous'按鈕的功能只會低到1.它甚至不會回到帶有0標籤的文本字段。解決此問題的唯一方法是將所有標記值增加1,以便第一個文本字段從1開始而不是零。

這是我的代碼。我的代碼中有一個錯誤,我看不到?或者這是一個奇怪的問題與標籤?

-(void)gotoPrevTextfield{ 
// If the active textfield is the first one, can't go to any previous 
// field so just return. 
UITextField *textField = (UITextField *)[inputsView viewWithTag:0]; 
NSLog(@"%i",textField.tag); 
NSLog(@"%i",txtActiveField.tag); 
if (txtActiveField == textField) { 
    NSLog(@"returning at previous"); 
    return; 
} 
else { 
    NSLog(@"set responder"); 
    // Otherwise if a different textfield has the focus, the operation 
    // of "previous" button can be done and set the previous as the first 
    // responder. 
    textField = (UITextField *)[inputsView viewWithTag:txtActiveField.tag - 1]; 
    NSLog(@"%i",textField.tag); 
    NSLog(@"%i",txtActiveField.tag); 
    [textField becomeFirstResponder]; 
} 

} 

回答

2

注意,未設置標籤默認爲0,這樣幾乎是一個糟糕的選擇。你可能會得到另一種你並不期待的觀點。

一個相當不錯的做法是添加一些常量,如100,考慮使常量爲const int或#define以使清晰。

+0

Aaah。這可能正是發生了什麼事。 – 2012-01-31 19:52:34

+2

'enum'適用於建立名稱以表示一組接口項目的標籤。 @Jesse – 2012-01-31 20:10:48

+0

@Josh Yep,特別是如果有幾種類型的標籤。 – zaph 2012-01-31 20:15:40