Question

i created alert view,in alert view one text field will come but when i click return button on keyboard it doesn't disappear,even i add the delegate to .h file.

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
-(IBAction)barButtonPressed:(id)sender 
{ 
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Enter Data" message:@"\n\n\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];

textUserName =[[UITextField alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 200.0f, 40.0f)];
textUserName.placeholder = @"Name";
textUserName.autocorrectionType = UITextAutocorrectionTypeNo;
textUserName.textAlignment=UITextAlignmentLeft;
textUserName.userInteractionEnabled = YES;
textUserName.enabled = YES;
textUserName.enablesReturnKeyAutomatically= NO;
textUserName.clearsOnBeginEditing = NO;
textUserName.borderStyle = UITextBorderStyleRoundedRect;
textUserName.keyboardType = UIKeyboardTypeDefault;
textUserName.delegate = self;
//[textUserName setReturnKeyType:UIReturnKeyDone];
 **strong text**
[alert
 addSubview:textUserName];

[alert show];
[self resignFirstResponder];
}

thank you

Was it helpful?

Solution

You can use UIAlertViewStylePlainTextInput of UIAlertView to get text field in alertView :

UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Enter Data" message:@"\n\n\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];

// Set alertView style
alert.alertViewStyle = UIAlertViewStylePlainTextInput;

textUserName =[alert textFieldAtIndex:0];

// Then customize your textField
textUserName.placeholder = @"Name";
textUserName.autocorrectionType = UITextAutocorrectionTypeNo;
textUserName.textAlignment=UITextAlignmentLeft;
textUserName.userInteractionEnabled = YES;
textUserName.enabled = YES;
textUserName.enablesReturnKeyAutomatically= NO;
textUserName.clearsOnBeginEditing = NO;
textUserName.borderStyle = UITextBorderStyleRoundedRect;
textUserName.keyboardType = UIKeyboardTypeDefault;

[alert show];

In this no need of setting delegate separately..

OTHER TIPS

You need to set delegate property of UITextField after initializing it.

use:

textUserName =[[UITextField alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 200.0f, 40.0f)];
textUserName.delegate = self;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top