views:

542

answers:

2

I have two text fields in my view. I did it using IB. My problems are

  1. After entering the text in textField1 I am entering text in textField2. When I click in textField1 again the previous text is disappeared in the textField1.

  2. After entering the text in both the textFields, I need the keyboard to disappear. But, even I touched the return key in the keyboard layout or I touched the screen outside the text field the keyboard is not disappearing.

How can I make this. Thank you.

- (void)viewDidLoad 
{
    self.title = @"Edit";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(save)];

    //[nameField becomeFirstResponder];
    nameField.clearsOnBeginEditing = NO; //First text field
    [nameField resignFirstResponder];
    //[descriptionField becomeFirstResponder];
    descriptionField.clearsOnBeginEditing = NO; //second text dield
    [descriptionField resignFirstResponder];
    [super viewDidLoad];
}   

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == nameField) {
    [nameField resignFirstResponder];
    }
    else if(textField == descriptionField){
    [descriptionField resignFirstResponder];
    }
    return YES;
}

I did in the above way but the keyboard is still not disappearing. And after entering the text in textField1 when I press return key the cursor is not going to textField2. How can I make it work ?

Thank You.

+1  A: 

For problem 1, check the clearsOnBeginEditing property of the text field.

For problem 2, you can send the resignFirstResponder message to the text field.

See Managing Keyboard section in the UITextField documentation.

Laurent Etiemble
Thank You. I worked out it.
srikanth rongali
A: 

I got the result. First thing in my mistake is I did not add UITextFieldDelegate in interface. And the remaining I did the changes in code.

- (void)viewDidLoad {
    self.title = @"Edit";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(save)];

    [nameField becomeFirstResponder];
    nameField.delegate = self;
    descriptionField.delegate = self;

    [super viewDidLoad];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    NSLog(@"Return Key Pressed");
    if (textField == nameField) {
        [nameField resignFirstResponder];
        [descriptionField becomeFirstResponder];
    }
    else if(textField == descriptionField){
    [descriptionField resignFirstResponder];
    }
    return YES;
}

After entering the text in textField1 if I touch return the cursor goes to textFeild2. It is working good. But when I touch the screen out of text field the keyboard is not disappearing. Thank you.

srikanth rongali