带有两个TextFields和两个按钮的UIAlertView

Aks*_*hay 8 iphone objective-c uialertview ios

问题 - 我想要一个包含标题的UIAlertView,两个带占位符的文本字段和两个按钮. 我到目前为止做了什么 - 我已经使用了这段代码,我得到的确切的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);

}
Run Code Online (Sandbox Code Playgroud)

然后我尝试了一些未记录的api,它们给了我想要的结果,但我担心它可能会导致我的应用程序被拒绝.

那么,任何解决方案呢?

Fme*_*ina 61

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

可能的值:

  • 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];
Run Code Online (Sandbox Code Playgroud)

您可以访问alertView上的文本字段的值:clickedButtonAtIndex: UIAlertViewDelegate.

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


dan*_*anh 4

y==400 大约是手机屏幕的底部。然后我想, UIAlertView 从来没有期望添加子视图或它的框架设置,重置它的框架居中。你能注释掉最后一行并看看会发生什么吗?