yozhik
yozhik

Reputation: 5074

UITextField keyboard Issue

I have such a problem: I have a UITextField in my UITableViewCell. When I tap on that text field -> keyboard appears, but when I press Enter button keyboard don't disappear. I need such a behavior for my text field and keyboard:

  1. When I pressed Enter, Esc - keyboard must disappear.

Upvotes: 0

Views: 1993

Answers (7)

karthikeyan
karthikeyan

Reputation: 3898

try this

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

or this is for anywhere in the view

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

Upvotes: 0

Ankit Jain
Ankit Jain

Reputation: 1886

Write this code to create UITextLabel

    UITextField *username = [[UITextField alloc]initWithFrame:CGRectMake(10.0f, 10.0f, 110.0f, 30.0f)]
    [username setReturnKeyType:UIReturnKeyNext];
    [username setDelegate:self];
    [self.view addSubview:username];

Now to resign write this code.

-(void)resignKeyboard
{
    if([username isEditing])
    {
        [username resignFirstResponder];
    }
}

I hope it works for you.

Upvotes: 0

RyanTCB
RyanTCB

Reputation: 8234

This may be an old post but I found it searching for the answer so chances are someone else might so don't shoot me for posting.

Just wanted to add don't forget to make the delegate connection in IB for the UITextField

Upvotes: 1

iphony
iphony

Reputation: 536

Try this


[txtField setReturnKeyType:UIReturnKeyDone];

txtField.enablesReturnKeyAutomatically=YES;

Upvotes: 1

yozhik
yozhik

Reputation: 5074

@interface Untitled2ViewController : UIViewController <UITextFieldDelegate>
{
    IBOutlet UITextField *text;
}

@property (nonatomic, retain) IBOutlet UITextField *text;
@end

//m file

#import "Untitled2ViewController.h"

@implementation Untitled2ViewController
@synthesize text;

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

In xib file I set: Return Key: Done, Auto-anabling return key. I also tried without it, and still no reaction. Keyboard don't hides.

Upvotes: 0

Swapna
Swapna

Reputation: 2235

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

Where textField is UITextField in UITableViewCell

Upvotes: 1

Vladimir
Vladimir

Reputation: 170849

Implement textFieldShouldReturn: method in textField's delegate and call [textField resignFirstResponder] there - that will hide keyboard when return key is pressed.

I'm not sure if that will work for 'Esc' as well, but there's no such key on real device anyway so it must not be a problem

Upvotes: 1

Related Questions