关于UIAlertController 的一些用法

来源:互联网 发布:淘宝网店手机可以开吗? 编辑:程序博客网 时间:2024/05/18 23:27

1.设置两个UIAlertController

@property (nonatomic,strong)UIAlertController *alert;

@property (nonatomic,strong)UIAlertController *alert2;

2.通过懒加载创建

-(UIAlertController *)alert

{

   if (_alert ==nil) {

        _alert = [UIAlertControlleralertControllerWithTitle:@"标题"message:@"可以自定义一些提示框"preferredStyle:UIAlertControllerStyleActionSheet];

        //UIAlertControllerStyleActionSheet

        //UIAlertControllerStyleAlert

/*

 UIAlertActionStyleDefault = 0,

 UIAlertActionStyleCancel,

 UIAlertActionStyleDestructive

 */

        

        UIAlertAction *action1 = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

        UIAlertAction *action2 = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:nil];

        UIAlertAction *action3 = [UIAlertActionactionWithTitle:@"action3"style:UIAlertActionStyleDestructivehandler:nil];

        UIAlertAction *action4 = [UIAlertActionactionWithTitle:@"action4"style:UIAlertActionStyleDestructivehandler:nil];


        [_alertaddAction:action1];

        [_alertaddAction:action2];

        [_alertaddAction:action3];

        [_alertaddAction:action4];

    }

   return _alert;

}


3.当点击button时候

- (IBAction)btn1:(id)sender {

显示提示框

    [selfpresentViewController:self.alertanimated:YEScompletion:nil];

}


4.添加textfield

-(UIAlertController *)alert2

{

   if (_alert2 ==nil) {

        _alert2 = [UIAlertControlleralertControllerWithTitle:@"标题"message:@"可以自定义一些提示框"preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *action1 = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

        UIAlertAction *action2 = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:nil];

        [_alert2addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {

            textField.placeholder =@"密码";

        }];

        [_alert2addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {

            textField.placeholder =@"账号";

        }];

                                  [_alert2addAction:action1];

                                  [_alert2addAction:action2];

    }

    NSLog(@"----------------->>>");

    return_alert2;

}

5.点击按钮的时候显示textfield提示框 然后再textfield可以做一些操作

- (IBAction)btn2:(id)sender {

    [selfpresentViewController:self.alert2animated:YEScompletion:nil];

}





0 0