iPhone keyboard

Member
Posts: 241
Joined: 2008.07
Post: #1
I was wondering how does the iPhone keyboard work.

I figure it probably belongs to a view and is usually sent a message by a field and pops up like a drawer when it receives the message.

What view does it belong to?

My game view is inherited from UIView but I'm running OpenGL, so is it possible to have a keyboard pop up over an OpenGL view?

Thanks again.
Quote this message in a reply
Member
Posts: 45
Joined: 2008.04
Post: #2
Add a UITextField, then send a becomeFirstResponder message to it so as to popup the keyboard immediately.

The crashlanding sample code shows this working.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #3
Yeah, it works fine over GL views for me.

... I may be way off here, so I don't mean to add dis-information, but for some reason I don't get the impression that it belongs to any view in particular, but rather the system itself, somehow. It seems to operate above everything.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #4
It belongs to the instance of UIWindow. UIWindow keeps track of the first responder. And there are a few other clues in UIWindow.h, as well, including the definitions of all of the UIKeyboard*Notifications.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #5
Yeah, but it seems like it is a special member since it seems to draw with a much higher priority than say, an underlying GL view. Just a goofy side-ways observation on my part from all the hundreds of hours of watching things during development. Wacko
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #6
Of course it can have a higher priority, you have one UIWindow instance and every single thing you see on your screen is a subview of that UIWindow. The UIWindow is the top of any view hierarchy on the iPhone, so it has the ability to insert any view anywhere it wants.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #7
Sorry, I understand that. I've just noticed some subtle inconsistencies with how things seem to be timed to draw on iPhone from time to time. Maybe has to do with scheduling with the PVR hardware.

Anyway, yeah, it belongs to the UIWindow.
Quote this message in a reply
Member
Posts: 241
Joined: 2008.07
Post: #8
The stacking of levels takes precedence over the stacking of windows within each level. That is, even the bottom window in a level obscures the top window of the next level down. Levels are listed in order from lowest to highest.

- Found this in the documentation for UIWindow. Is that what you were unsure about Jake?

Also, strangely, the UIWindow inherits the UIView. My view was inheriting from UIView so I changed it to UIWindow, since it's up the hierarchy. It compiles fine but I'm not messing with it anymore tonight. I'll see if I can figure out how to get a keyboard up tomorrow. Thanks everyone for your inputs.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #9
bmantzey Wrote:The stacking of levels takes precedence over the stacking of windows within each level. That is, even the bottom window in a level obscures the top window of the next level down. Levels are listed in order from lowest to highest.

- Found this in the documentation for UIWindow. Is that what you were unsure about Jake?

Thanks but no, I was just making a strange observation that the keyboard *appears* to me to get to play outside the rules even though it belongs to UIWindow. My observation has zero bearing on reality, and did not directly refer to the question at hand, "What view does it belong to?", so I should have left it out. Sorry about the confusion.
Quote this message in a reply
Member
Posts: 241
Joined: 2008.07
Post: #10
How would I begin setting up the UITextField if I'm not loading from xib/nib?
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #11
Something like this (I added some extra settings):

Code:
myTextField = [[UITextField alloc] initWithFrame:MyCGRect];
[myTextField setDelegate:self];
[myTextField setBorderStyle:UITextBorderStyleRoundedRect];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[myTextField setTextColor:[UIColor blackColor]];
[myTextField setClearButtonMode:UITextFieldViewModeAlways];
[myTextField setFont:[UIFont fontWithName:@"Arial" size:18.0f]];
[myTextField setPlaceholder:@"Tap here to edit"];
[myTextField setTextAlignment:UITextAlignmentCenter];
[myTextField setReturnKeyType:UIReturnKeyDone];
[myView addSubview:myTextField];
Quote this message in a reply
Member
Posts: 241
Joined: 2008.07
Post: #12
In this case, what is self?
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #13
Whatever you want it to be, but it has to conform to the <UITextFieldDelegate> protocol. It can be myView, or whatever. I have a global game instance that I often use as a catch-all delegate for all kinds of things like this because most of my games on iPhone are C. So like I said, the delegate could be anything.

[adding] as mentioned above, check out CrashLanding to see it in action
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #14
Here are some delegate method implementations to look at:
Code:
- (void)textFieldDidEndEditing:(UITextField*)textField
{
    NSLog([textField text]);
    [textField endEditing:YES];
    [textField removeFromSuperview];
}

- (BOOL)textFieldShouldReturn:(UITextField*)texField
{
    // end editing
    [texField resignFirstResponder];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // limit to 14 characters for fun
    if ([[textField text] length] < 14 || [string length] == 0)
        return YES;
    else
        return NO;
}
Quote this message in a reply
Member
Posts: 249
Joined: 2008.10
Post: #15
AnotherJake Wrote:Here are some delegate method implementations to look at:
Code:
- (void)textFieldDidEndEditing:(UITextField*)textField
{
    NSLog([textField text]);
    [textField endEditing:YES];
    [textField removeFromSuperview];
}

- (BOOL)textFieldShouldReturn:(UITextField*)texField
{
    // end editing
    [texField resignFirstResponder];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // limit to 14 characters for fun
    if ([[textField text] length] < 14 || [string length] == 0)
        return YES;
    else
        return NO;
}



Hi AnotherJake, could you explain me more or less what I should do to use keyboard in a OpenGL and C++ game (based on glpaint demo)? Is that possible? I would not like an edit field, just the keyborad in lanscape mode.

Thanks a lot for your help.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  on-screen keyboard help Merax 1 1,698 Jul 26, 2010 08:21 AM
Last Post: ThemsAllTook