2014-03-19 26 views
12

我有一個包含UITableView中包含的多個輸入字段的窗體屏幕。如果用戶連接藍牙鍵盤,則他可以按「Tab」鍵。對於每個文本字段,textFieldShouldBeginEditing方法的問題會被多次調用。這是正常的行爲嗎?正常的行爲是,如果某個字段處於焦點狀態,並且用戶按下標籤,則光標應該跳轉到某個其他文本字段,因此textFieldShouldBeginEditing只會被調用一次(對於此文本字段)。當按下「Tab」鍵時,多次調用textFieldShouldBeginEditing

看起來像這個問題沒有解決(post1,post2)。你們是否忽略了這個問題的存在,或者找到了解決辦法?

+0

能否請您發表以下的輸出: - (BOOL)textFieldShouldBeginEditing:(*的UITextField)文本框{ 的NSLog(@ 「文本字段:%@」,文本框); } –

+1

@SebastianBorggrewe它將打印不同的textField對象,因此它會針對每個不同的文本字段進行調用。已經做到了。 – Centurion

+1

檢查:[link](http://weaklyreferenced.wordpress.com/2012/11/13/responding-to-the-tab-and-shift-tab-keys-on-ios-5-ios-6-with鍵盤指針 – staticVoidMan

回答

1

我有一個UIViewController我在那裏聽UITextFieldDelegate textFieldShouldBeginEditing,並且只對我的一個文本框有一個特別的操作。所以當打在藍牙鍵盤上的Tab時,會導致特殊情況發生。

今天,我終於找到asolution:

我報名參加Tab鍵keyCommand,然後讓它使用類別上UIResponder找到firstResponder(目前的文本字段),然後發射回通過委託方法。

你首先需要這個類別,以獲得firstResponder: https://stackoverflow.com/a/21330810/747369

然後,只需註冊keyCommand並獲得當前firstResponder。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [self addKeyCommand:[UIKeyCommand keyCommandWithInput:@"\t" modifierFlags:0 action:@selector(tabKeyPressed:)]]; 
} 

- (void)tabKeyPressed:(UIKeyCommand *)sender 
{ 
    id firstResponder = [UIResponder currentFirstResponder]; 
    if ([firstResponder isKindOfClass:[UITextField class]]) 
    { 
     UITextField *textField = (UITextField *)firstResponder; 
     // Call the delegate method or whatever you need 
     [self textFieldShouldReturn:textField]; 
    } 
} 
+0

我最近發現,這在使用Shift + Tab時不起作用。這將需要具有適當的modifierFlags的另一個KeyCommand。 – neonhomer

相關問題