views:

1498

answers:

3

is it possible to bring up the keyboard in an iphone app without a textview? or will i have to have an invisible textview?

if so, how do you programatically create a textview and then bring up the keyboard (without the user having to tap the textview)? the only examples i can find use interface builder..

+5  A: 

The only (valid) way to show the keyboard is to have a textfield that is first responder. You can hide it and make it first responder programmatically by calling becomeFirstResponder on the hidden textfield.

You can create a UITextView programmatically by doing something like this (assume aRect and view exist)

var textView = [[[UITextView alloc] initWithFrame:aRect] autorelease];
[view addSubview:textView];

[textView becomeFirstResponder];
klaaspieter
+1  A: 

The way this stuff works is via the NSNotificationCenter publish/subscribe model. First you need to use addObserver:selector:name:object:, then you can try doing this:

[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:NSTextDidBeginEditingNotification object:self]];

But I'm not sure what notifications you would get, or would need to register for, to get the keyboard typing character values. Good luck and happy hacking :)

slf
How will this bring up the keyboard?
klaaspieter
I'm not sure if it will, the trick is to find the right notification to post
slf
more keyboard notification stuff http://www.iphonedevsdk.com/forum/iphone-sdk-development/6275-add-toolbar-top-keyboard.html
slf
A: 

After some more digging, I found this. It's unofficial, but I bet it works.

UIKeyboard *keyboard = [[[UIKeyboard alloc] initWithFrame: CGRectMake(0.0f, contentRect.size.height - 216.0f, contentRect.size.width, 216.0f)] autorelease];
        [keyboard setReturnKeyEnabled:NO];
        [keyboard setTapDelegate:editingTextView];
        [inputView addSubview:keyboard];
slf