Resign не работает в UITextField UIAlertView?

я создал представление предупреждений, в представлении предупреждений появится одно текстовое поле, но когда я нажимаю кнопку возврата на клавиатуре, оно не исчезает, даже если я добавляю делегата в файл .h.

-(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];
}

благодарю вас


person Sri..    schedule 02.01.2013    source источник
comment
попробуйте установить делегата из раскадровки/построителя интерфейса   -  person Anoop Vaidya    schedule 02.01.2013


Ответы (2)


Вы можете использовать UIAlertViewStylePlainTextInput из UIAlertView, чтобы получить текстовое поле в 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];

В этом нет необходимости устанавливать делегата отдельно.

person Anusha Kottiyal    schedule 02.01.2013
comment
@ Ануша, возможно, он говорит о стиле просмотра предупреждений! - person DD_; 02.01.2013

Вам нужно установить свойство делегата UITextField после его инициализации.

использовать:

textUserName =[[UITextField alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 200.0f, 40.0f)];
textUserName.delegate = self;
person Rahul Wakade    schedule 02.01.2013