views:

1233

answers:

2

Hi,

it is simple to set the Return key programmatically:
textField.returnKeyType = UIReturnKeyDone; // This is in the loadView/init-method

However, it seems that it is not so easy to toggle that key, i.e. change it from Done to something else depending on, for example, what is currently shown in the text field! That is, a subsequent statement in a callback method:
textField.returnKeyType = UIReturnKeyRoute;
does not change the keys title from Done to Route!?!?

Has anyone else observed this? Any workaround?

Regards,
/John

+2  A: 

If by "does not work" you mean it doesn't do anything perhaps you need something like this?

- (BOOL)textFieldShouldReturnUITextField *)theTextField
{   
[theTextField resignFirstResponder];
return YES;
}

If by "does not work" you mean that the text is wrong, as far as I know you can only select from the set of predefined buttton types that the API provides. I don't know of any way to directly set that text. There may be a way with private apis (see EricaSadun.com)

slf
A: 

Hi I am also having same problem keyboard is not disappear after pressing "Done" i am using Cocos2d+UIKit . if any help or sample code is appreciate please find my code here

import "TFAppDelegate.h"

UITextField *returnTextField;

@implementation MYSCENE -(id) init { [super init];

[self createTextField];

return self;

}

  • (UITextField *)createTextField { CGRect frame = CGRectMake(100.0, 100.0, 100.0, 20.0); returnTextField = [[UITextField alloc] initWithFrame:frame];

    returnTextField.borderStyle = UITextBorderStyleBezel; returnTextField.textColor = [UIColor blackColor]; returnTextField.font = [UIFont systemFontOfSize:10.0]; returnTextField.placeholder = @""; returnTextField.backgroundColor = [UIColor whiteColor]; returnTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support

    returnTextField.keyboardType = UIKeyboardTypeDefault; // use the default type input method (entire keyboard) returnTextField.returnKeyType = UIReturnKeyDone;

    //returnTextField.enablesReturnKeyAutomatically = YES; returnTextField.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right

    return returnTextField; }

  • (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    [theTextField resignFirstResponder]; return YES; }

-(void) dealloc { [returnTextField release]; [super dealloc]; } @end

@implementation TFAppDelegate

  • (void)applicationDidFinishLaunching:(UIApplication *)application { // NEW: Init the window window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [window setUserInteractionEnabled:YES]; //[window setMultipleTouchEnabled:YES];

    //[[Director sharedDirector] setLandscape: YES]; [[Director sharedDirector] setDisplayFPS:YES];

    [[Director sharedDirector] attachInWindow:window];

    Scene *scene = [Scene node]; [scene add: [MYSCENE node]];

    [[[Director sharedDirector]openGLView]addSubview:returnTextField]; [window makeKeyAndVisible];

    [[Director sharedDirector] runWithScene: scene];

} -(void)dealloc { [super dealloc]; } -(void) applicationWillResignActive:(UIApplication *)application { [[Director sharedDirector] pause]; } -(void) applicationDidBecomeActive:(UIApplication *)application { [[Director sharedDirector] resume]; } - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { [[TextureMgr sharedTextureMgr] removeAllTextures]; }

@end