UIAlertView 显示文本框和按钮



我使用以下代码创建了一个UIAlertView并在其上添加了一些组件,但结果是在图像:(((图像在这里:https://i.stack.imgur.com/DTg02.png(

-(IBAction)login:(id)sender
{
    UIAlertView *login = [[UIAlertView alloc]initWithTitle: @"Login first"
                                                   message:@"enter username and password please first" delegate:self cancelButtonTitle:@"cancel"otherButtonTitles:@"OK", nil];
k = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 70.0, 200.0, 25.0)]; //<< it also displays wrong without this line
k.text = @"";
k.backgroundColor = [UIColor whiteColor];
k.clearButtonMode= UITextFieldViewModeWhileEditing;
k.keyboardType = UIKeyboardTypeAlphabet;
k.keyboardAppearance = UIKeyboardAppearanceAlert;
p = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 100.0, 200.0, 25.0)];
p.text = @"";
p.backgroundColor = [UIColor whiteColor];
p.clearButtonMode = UITextFieldViewModeWhileEditing;
p.keyboardType = UIKeyboardTypeAlphabet;
p.keyboardAppearance = UIKeyboardAppearanceAlert;
[login addSubview:k];
[login addSubview:p];
[login show];
}

如果要在UIAlertView上添加多个textField和多个按钮,请按照以下步骤操作:

步骤 1 :

@interface ViewController ()<UIAlertViewDelegate>
{
    UITextField *textFieldOne,*textFieldTwo,*textFieldThird;
} 

步骤 2 :

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Multiple TextField" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
    [alert addButtonWithTitle:@"CANCEL"];
     alert.delegate=self;
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    textFieldOne=[[UITextField alloc]initWithFrame:CGRectMake(5, 0, 150, 20)];
    [textFieldOne setPlaceholder:@"First Name"];
    [myView addSubview:textFieldOne];
    textFieldTwo=[[UITextField alloc]initWithFrame:CGRectMake(5,25, 150, 20)];
    [textFieldTwo setPlaceholder:@"Middle Name"];
    [myView addSubview:textFieldTwo];
    textFieldThird=[[UITextField alloc]initWithFrame:CGRectMake(5,50, 150, 20)];
    [textFieldThird setPlaceholder:@"Last Name"];
    [myView addSubview:textFieldThird];
    [alert setValue:myView forKey:@"accessoryView"];
    [alert show];

第三步:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"YES"])
    {
        NSLog(@"Button YES");
        NSLog(@"TextFiledOne Text = %@",textFieldOne.text);
        NSLog(@"TextFiledTwo Text = %@",textFieldTwo.text);
        NSLog(@"TextFiledThree Text = %@",textFieldThird.text);
    }
    else if([title isEqualToString:@"NO"])
    {
        NSLog(@"Button NO");
    }
    else
    {
        NSLog(@"Cancel is click");
    }
}

UIAlertView不考虑额外的文本输入。您可以改用此处所述的UIActionSheet

没有必要添加到已经添加在此处的代码中,因为它已经足够好了,并且给出了一些不错的替代方案。我要做的是告诉你为什么你不应该使用addSubview:将自己的UITextField添加到UIAlertView中。

基本上,Apple 已经将UIAlertView类设置为as is使用,并且此类的视图层次结构是私有的。

子类注释

UIAlertView 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。

从 Apple 文档中获取UIAlertView子类注释。

从本质上讲,这意味着如果您在UIAlertView上使用addSubview:方法,那么您实质上是在更改Apple指示为私有的内容,并且您的应用程序将从规则2.5的Apple应用程序审核流程中被拒绝。

2.5 使用非公共 API 的应用将被拒绝

您可能会问自己为什么这种方法甚至存在UIAlertView肯定因为它在那里我们可以使用它。好吧,不,它存在的原因是因为UIAlertView类本身就是UIView的子类,这是声明addSubview:方法的地方。不幸的是,没有理由阻止UIAlertView实例实际调用该方法。因此,苹果所做的是他们覆盖了addSubview:方法,它所做的只是返回。因此,此方法使其不执行任何操作,并且您传递给此方法的任何视图都不会被添加,因为它永远不会调用[super addSubview:view];

因此,当涉及到UIAlertView时,有两件事你不应该做,它们是:

  1. 子类UIAlertView类似@interface MYAlertView : UIAlertView这是子类,是不允许的。

  2. [alertView addSubview:view];一样更改视图层次结构

但是,我应该指出一点,虽然我们不允许子类UIAlertView但我们仍然可以像@interface UIAlertView (MyCategory)一样为它创建类别,因为这不被归类为子类,它被称为类类别或类扩展(我以前也听说过它被称为类提供者(。

还应该注意的是,如果您正在为iOS8及以上版本进行开发,则应使用 UIAlertController,因为UIAlertView已被弃用。

重要:

UIAlertView 在 iOS 8 中已弃用。(请注意,UIAlertViewDelegate 也被弃用。要在 iOS 8 及更高版本中创建和管理警报,请改用 UIAlertController 和首选样式的 UIAlertControllerStyleAlert。

因此,我要么使用

其他答案中提到的自定义类之一,要么转向使用UIAlertController,或者如果您有时间,请创建自己的自定义警报视图。

我希望这个解释能帮助你更多地了解UIAlertView

不要虚张声势。只需使用以下代码:

[login setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];

要处理用户的输入,请使用以下命令:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if([[alertView textFieldAtIndex:0].text isEqual:@"UserName"]&&[[alertView textFieldAtIndex:1].text isEqual:@"PassWord"])
    {
        //do your stuff here
        [adminLoginButton setTitle:@"Logout" forState:UIControlStateNormal];
        [adminLoginButton setImage:[UIImage imageNamed:@"Logout.png"] forState:UIControlStateNormal];
    }
    else
    {
        UIAlertView *errorMessage = [[UIAlertView alloc]initWithTitle:@"Authentication Error" message:@"Input is wrong" delegate:self cancelButtonTitle:@"Okay" otherButtonTitles: nil];
        [errorMessage show];
    }
}

最新更新