开发者

Tracking the firstResponder object

I need to keep track of which text field is the firstResponder for my custom keyboard to work. In the code below, I have grossly oversimplified my program, but here is the gist of the problem:

@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 setDelegat开发者_JAVA百科e: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;
}

If I remove the line setting the firstResponder to nil, the code ceases to function properly, but I am not sure why. (Without that line, I can select the first textView to bring up the keyboard, but after that I can never bring the keyboard back. Any ideas? Thanks!


I'm not sure that I understand why firstResponder needs to be kept track of for a custom keyboard to work. I use a custom keyboard without knowing what the first responder is.

Do you use:

textView.inputView = pickerKeyboard

How about the following, called on the view to resign the first responder:

[self.view endEditing:NO];


I have had a similar problem and I have just figured out the issue. Somewhere in some part of Apple's first responder code, they are using a selector named firstResponder. When you created the property firstResponder you inadvertently overrode that selector. That will cause Apple's code to fail. This, in my humble opinion, is a bug in Apple's framework, and the firstResponder method isn't documented anywhere. Name your property myFirstResponder or anything else and everything should work just fine.

See Why does the keyboard not show when a view is popped from the navigation stack?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜