I'm working on an app that at some point requires keyboard input. My app uses OpenGL ES for display, and I have my own graphics framework that can display a text field, and knows how to manage the cursor and render text. This code works great on other platforms that have a physical keyboard, like Windows and OS X.
All I need to get the iOS version of my app working is to be able to bring the keyboard up and down programmatically, and also to get the key press events from the user into my view so that I can then route them into my framework's own event system.
I saw this question, but I could not make the solution depicted there work. Not sure if it is because I'm doing something wrong, or because it doesn't work on current iOS releases.
EDIT: It would be as helpful to be pointed at a working app with source code that creates any UI 开发者_开发技巧element programmatically and displays it on top of a GL ES screen. I think I can figure the rest out if I get that part understood.
I can't point you at a working example, but I believe what you're looking for is here http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIKeyInput_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UIKeyInput
The basic idea is that you implement the UIKeyInput
protocol on a UIResponder
object, such as a UIView
. Make the view the firstResponder and the keyboard should automatically appear. The UIKeyInput
protocol gives you simple feedback for inserting a character and deleting a character when the user presses buttons on the keyboard.
This isn't working code, but it would look something like this:
@interface MyKeyboardView : UIView <UIKeyInput>
@end
@implementation MyKeyboardView
- (void)insertText:(NSString *)text {
// Do something with the typed character
}
- (void)deleteBackward {
// Handle the delete key
}
- (BOOL)hasText {
// Return whether there's any text present
return YES;
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
@end
when the view is attched, make it the first responder to show the keyboard
[myView becomeFirstResponder];
or resign first responder to dismiss the keyboard
[myView resignFirstResponder];
Edit: make sure your view is attached to the view hierarchy or this probably won't do anything.
精彩评论