2010-10-02 266 views
2

我需要跟蹤哪個文本字段是我的自定義鍵盤的firstResponder才能工作。在下面的代碼,我已經嚴重過於簡化我的程序,但這裏的問題要點:跟蹤firstResponder對象

@implementation SimplePickerViewController 
@synthesize pickerKeyboard; 
@synthesize textView; 
@synthesize textView2; 
@synthesize firstResponder; 

-(void)viewDidLoad{ 
    pickerKeyboard = [[PickerKeyboardViewController alloc] initWithNibName:@"PickerKeyboard" bundle:nil]; 
    pickerKeyboard.delegate = self; 
    [self.textView setInputView:pickerKeyboard.view]; 
    [self.textView setDelegate:self]; 
    [self.textView2 setInputView:pickerKeyboard.view]; 
    [self.textView2 setDelegate:self]; 
} 

-(void)hideKeyboard{ 
    [self.firstResponder resignFirstResponder]; 
    self.firstResponder = nil; //without this line, the code doesn't work. 
} 
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{ 
    self.firstResponder = textView; 
    [self.pickerKeyboard.picker reloadAllComponents]; 
    return YES; 
} 

如果我刪除行firstResponder設置爲零,代碼停止正常工作,但我不確定爲什麼。 (沒有那條線,我可以選擇第一個textView來調出鍵盤,但是之後我永遠不能把鍵盤拿回來。任何想法?謝謝!

回答

3

我不確定我明白爲什麼firstResponder需要。持續跟蹤的自定義鍵盤工作,我使用自定義鍵盤不知道第一個響應是什麼

你使用:

textView.inputView = pickerKeyboard 

如何以下,呼籲以辭職急救人員:

[self.view endEditing:NO]; 
2

我有類似的問題,我剛纔發現了這個問題。 Apple的第一個響應者代碼的某個部分某處使用了名稱爲firstResponder的選擇器。當您創建屬性firstResponder時,您無意中覆蓋了該選擇器。這會導致Apple的代碼失敗。在我看來,這是Apple框架中的一個錯誤,並且firstResponder方法在任何地方都沒有記錄。命名您的財產myFirstResponder或其他任何東西,一切都應該工作得很好。

請參閱Why does the keyboard not show when a view is popped from the navigation stack?

+0

你真棒!這是我確切的問題。 – 2013-07-26 14:15:53