Youaregreat
Youaregreat

Reputation: 69

Resign keyboard of textfield present in alertview

I have three text fields on an alertview, I set keyboard as decimalType ,

- (IBAction)heightMethod:(id)sender

{

 self.utextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; utextfield.placeholder = @"  Centimeters";

self.utextfield.delegate=self;

self.utextfield.tag=3;
[ self.utextfield setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview: self.utextfield];
// Adds a password Field
self.ptextfield = [[UITextField alloc] initWithFrame:CGRectMake(40, 80.0, 80, 25.0)]; ptextfield.placeholder = @"   Feet";
self.ptextfield.delegate=self;
self.ptextfield.tag=4;

[self.ptextfield setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview:self.ptextfield];

self.ptextfieldInches = [[UITextField alloc] initWithFrame:CGRectMake(140, 80.0, 80, 25.0)]; ptextfieldInches.placeholder = @"   Inches";
self.ptextfieldInches.delegate=self;
self.ptextfieldInches.tag=5;


[ptextfieldInches setBackgroundColor:[UIColor whiteColor]];
[self.alertHeight addSubview:ptextfieldInches];

 [self.utextfield setKeyboardType:UIKeyboardTypeDecimalPad];
 [self.ptextfieldInches setKeyboardType:UIKeyboardTypeDecimalPad];
 [self.ptextfield setKeyboardType:UIKeyboardTypeDecimalPad];

[self.alertHeight show];

}

As I tap any textfield, keyboard resign only two times, but on third time its not resigning . I added resignfirst responder method inside the delgate method of alertview , look here

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

Upvotes: 0

Views: 152

Answers (3)

hariszaman
hariszaman

Reputation: 8424

When you Press OK button on alert view the keypad dismisses automatically.I have tested it out so remove resignfirstresponder from the above mentioned method and try

Upvotes: 0

Mathew Varghese
Mathew Varghese

Reputation: 4527

Try

- (void) alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    [self.view endEditing: YES];
}

Upvotes: 0

simbesi.com
simbesi.com

Reputation: 1529

Create iVar of UITextField and assign in side UITextFieldDelegate method textFieldShouldBeginEditing. Hopefully it should work.

Like Below:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
   textField = iVar;
}

Upvotes: 1

Related Questions