0

我创建了警报视图,在警报视图中会出现一个文本字段,但是当我单击键盘上的返回按钮时它不会消失,即使我将委托添加到 .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];
}

谢谢你

4

2 回答 2

3

您可以使用UIAlertViewStylePlainTextInputUIAlertView获取 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];

在此无需单独设置委托..

于 2013-01-02T11:51:31.063 回答
0

您需要UITextField在初始化后设置委托属性。

采用:

textUserName =[[UITextField alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 200.0f, 40.0f)];
textUserName.delegate = self;
于 2013-01-02T12:03:57.757 回答