我有一個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];
}
}
能否請您發表以下的輸出: - (BOOL)textFieldShouldBeginEditing:(*的UITextField)文本框{ 的NSLog(@ 「文本字段:%@」,文本框); } –
@SebastianBorggrewe它將打印不同的textField對象,因此它會針對每個不同的文本字段進行調用。已經做到了。 – Centurion
檢查:[link](http://weaklyreferenced.wordpress.com/2012/11/13/responding-to-the-tab-and-shift-tab-keys-on-ios-5-ios-6-with鍵盤指針 – staticVoidMan