开发者

How to reset keyboard view from 'symbols' to 'alphabet' programmatically

I have a UITextView and I am listening to a UIButton event to take action on the text inside this textView:

- (IBAction)buttonClicked {
    NSString *text = myTextView.text;
    //Do some work with the text.
    myTextView.text = @""; //reset the textView.
}

I don't want the textView to resignFirstResponder. The problem I am facing is: When the last entered character was a symbol, the keyboard is stuck on that view and is not reset to the default keyboard alphabetic view, as is the behavior in messaging app and other apps too. How do I reset the keyboard programmatically in this method?

This may be a repeat question for this question. But the answer doesn't seem to work for me.

This is the accepted answer:

[editingField setKeyboardType:UIKeyboardTypeNumberPad];
[editingField reloadInputViews];

And this is the last comment:

OK, apparently declaring it as a UITextView instead of a UIResponder affects how it behaves during runtime. That is literally the only change I made and it works now. Thanks.

But I can't seem to understand what the small change was that made it work, since I am still struggling with the issue.

I have already tried resignFirstResponder and becomeFirstResponder, but in quick succession, the scrollableView behind is not set according to keyboardWillShow event. I have also tried [textView setKeyboardAp开发者_如何学JAVApearance:UIKeyboardAppearanceDefault] but there is no change still.

Is there any API method that does this thing (like the keyboard is reset on clicking 'space' automatically)?


A hack that I used was to use a hidden textField to reload the inputView.

Code:

UITextField *hiddenTextField = [[UITextField alloc] init];
[superView addSubview:hiddenTextField];

[hiddenTextField becomeFirstResponder];
[hiddenTextField reloadInputViews];
[textView_ becomeFirstResponder];

[hiddenTextField removeFromSuperview];
[hiddenTextField release];


I don't use a temporary text field. I just resignFirstResponder and immediately becomeFirstResponder on the very textview. If the two calls are done immediately one right after the other, no keyboard sliding animation occurs.

So my buttonClicked function would look like this:

- (IBAction)buttonClicked {
    NSString *text = myTextView.text;
    //Do some work with the text.
    myTextView.text = @""; //reset the textView.

    // Reset keyboard back to QWERTY side
    [myTextView resignFirstResponder];
    [myTextView becomeFirstResponder];
}

As a side note, unfortunately, even though no animation occurs, the keyboard notifications (eg. UIKeyboardWillShowNotification, UIKeyboardWillHideNotification, etc) still get called. I simply set a flag before I resignFirstResponder to notify my keyboard hide/show notification callbacks to skip execution. And after I becomeFirstResponder, I clear that flag so when the keyboard does actually slide away, the notification callbacks can execute properly.


self.textField.keyboardType = UIKeyboardTypeAlphabet;
[self.textField reloadInputViews];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜