2013-01-02 56 views
0

我創建的警報視圖,在警報視圖一個文本字段會來,但是當我點擊鍵盤上的返回按鈕也不會消失,即使我添加了委託.h文件中。Resign在UIAlertView的UITextField中不工作?

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

謝謝

+0

嘗試從情節提要/界面生成 –

回答

3

您可以使用UIAlertViewUIAlertViewStylePlainTextInput獲得在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]; 

在此沒有必要單獨設置委託..

+0

設置代表什麼ü回答是真正優秀的,但外觀不是很好 – srinadh

+0

@ srinadh588外觀? –

+0

@Anusha他可能會談論警報視圖風格! –

0

您需要初始化後設置UITextField的代理屬性。

使用:

textUserName =[[UITextField alloc] initWithFrame:CGRectMake(50.0f, 50.0f, 200.0f, 40.0f)]; 
textUserName.delegate = self; 
相關問題