带有两个TextFields和两个button的UIAlertView

问题 – 我想要一个UIAlertView包含一个标题,两个带占位符和两个button的文本框。 到目前为止我所做的 – 我已经使用这个代码,我得到了所有这些我已经提到的确切的UIAlertView,但是当视图被加载时,似乎出现在底部的第一个,然后进入视图的中间。

-(void)showalert{ disclaimerAgreedAlertView = [[UIAlertView alloc] initWithTitle:@" " message:@" " delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Login", nil]; userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 15.0, 245.0, 25.0)]; userNameTextField.delegate=self; [userNameTextField setBackgroundColor:[UIColor whiteColor]]; [userNameTextField setKeyboardType:UIKeyboardTypeDefault]; userNameTextField.placeholder=@"Username"; userNameTextField.secureTextEntry=NO; [disclaimerAgreedAlertView addSubview:userNameTextField]; passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)]; passwordTextField.delegate=self; [passwordTextField setBackgroundColor:[UIColor whiteColor]]; [passwordTextField setKeyboardType:UIKeyboardTypeDefault]; passwordTextField.placeholder=@"Password"; passwordTextField.secureTextEntry=YES; [disclaimerAgreedAlertView addSubview:passwordTextField]; disclaimerAgreedAlertView.tag=99; [disclaimerAgreedAlertView show]; disclaimerAgreedAlertView.frame= CGRectMake(5, 400, 320, 160); } 

然后,我尝试了一些没有logging的api,它们给了我想要的结果,但是恐怕这可能会导致我的应用程序被拒绝。

那么,有什么解决办法呢?

y == 400是关于手机屏幕的底部。 我想象一下,UIAlertView从来没有预期要么添加子视图或框架设置,重置的框架要居中。 你能评论一下最后一行,看看会发生什么吗?

从iOS 5开始,可以使用UIAlertView的属性alertViewStyle获取不同types的警报视图,也可以使用文本字段。

可能的值:

  • UIAlertViewStyleDefault
  • UIAlertViewStyleSecureTextInput
  • UIAlertViewStylePlainTextInput
  • UIAlertViewStyleLoginAndPasswordInput

您可以使用UIAlertViewStyleLoginAndPasswordInput获取带有两个文本字段的UIAlertView,然后您可以自定义textFields的一些属性:

  UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Your_Title" message:@"Your_message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; [av setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; // Alert style customization [[av textFieldAtIndex:1] setSecureTextEntry:NO]; [[av textFieldAtIndex:0] setPlaceholder:@"First_Placeholder"]; [[av textFieldAtIndex:1] setPlaceholder:@"Second_Placeholder"]; [av show]; 

您可以访问UIAlertViewDelegatealertView:clickedButtonAtIndex:上的文本字段的值。

 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSLog(@"1 %@", [alertView textFieldAtIndex:0].text); NSLog(@"2 %@", [alertView textFieldAtIndex:1].text); } 

只需在下面的show语句中设置警报的框架

我使用以下用于相同的目的

 -(void)alertView { UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Enter User & Password" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Login", nil]; alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; UITextField * alertTextField1 = [alert textFieldAtIndex:0]; alertTextField1.keyboardType = UIKeyboardTypeDefault; alertTextField1.placeholder = @"User"; UITextField * alertTextField2 = [alert textFieldAtIndex:1]; alertTextField2.keyboardType = UIKeyboardTypeDefault; alertTextField2.placeholder = @"Password"; [alert show]; } 

如果在第二个文本字段中不使用安全字符,则添加

 alertTextField2.secureTextEntry=NO; 

我在我的应用程序中使用DDAlertPrompt 。 这很容易,一个下降课程。