Search code examples
iosiphoneuitextfielduialertviewresignfirstresponder

iphone: keyboard is not hiding


I have a situation, I have an alertview with one textfield drawn in it programmatically

UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Welcome" message:@"Enter you name" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];

    [nameField setBorderStyle:UITextBorderStyleRoundedRect];
    [nameField setPlaceholder:@"Enter Owners name"];
    [nameField setTextAlignment:UITextAlignmentCenter];
    [nameField setDelegate:self];
    [nameField setBackgroundColor:[UIColor clearColor]];

    [alertView addSubview:nameField];
    CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, -80.00);
    [alertView setTransform: moveUp];

    [alertView show];
    [alertView release];

to hide the keyboard I have used this

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

[textField resignFirstResponder];
return YES;

}

but keyboard is not hiding. Please help me in this.


Solution

  • You can hide your keyboard using this. This will work when clicking the background of your simulator/emulator/iOS-device.

      -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
                [yourtextfield resignFirstResponder];
    
                // you can have multiple textfields here
    
    
            }