UIAlertController的用法示例

在iOS8中 我们熟悉的UIAlertView已经不被苹果提倡了 取而代之的是UIAlertController  关于详细的解释说明上一篇转载的文章里  已经很详细的介绍了  这篇文章 主要来自己敲一下代码 顺便看一下实现的效果有什么不同

 

//    1.Alert

    UIAlertController *controller = [UIAlertControlleralertControllerWithTitle:nilmessage:@"确定要注销吗"preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {

        [self.navigationControllerpopToRootViewControllerAnimated:YES];

    }];

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

    [controller addAction:okAction];

    [controller addAction:cancleAction];

    [selfpresentViewController:controller animated:YEScompletion:nil];

   实现的效果是这样的:



//    2.ActionSheet

    UIAlertController *controller = [UIAlertControlleralertControllerWithTitle:@"Tips"message:@"确定要注销吗"preferredStyle:UIAlertControllerStyleActionSheet];

    UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {

        [self.navigationControllerpopToRootViewControllerAnimated:YES];

    }];

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

    [controller addAction:okAction];

    [controller addAction:cancleAction];

    [selfpresentViewController:controller animated:YEScompletion:nil];


实现效果是这样的:


 //3.textfield

   #pragma 注意 如果想加入textfield 类型只能是StyleAlert

    UIAlertController *controller = [UIAlertControlleralertControllerWithTitle:@"Tips"message:@"请输入用户名密码"preferredStyle:UIAlertControllerStyleAlert];

    [controller addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {

        textField.placeholder  = @"用户名";

    }];

    [controller addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {

        textField.placeholder = @"密码";

        textField.secureTextEntry = YES;

    }];

    UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {

        [self.navigationControllerpopToRootViewControllerAnimated:YES];

    }];

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

    

    

    [controller addAction:okAction];

    [controller addAction:cancelAction];

    [selfpresentViewController:controller animated:YEScompletion:nil];

  实现效果是这样的:


最后加上对textfield用户名长度判断处理的代码

- (IBAction)logout:(UIBarButtonItem *)sender {

   #pragma 注意 如果想加入textfield 类型只能是StyleAlert

    controller = [UIAlertController alertControllerWithTitle:@"Tips" message:@"请输入用户名密码" preferredStyle:UIAlertControllerStyleAlert];

    

    /*增加一个监听*/

    [controller addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

        textField.placeholder  = @"用户名(长度至少为3)";

        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(usernameChanged) name:UITextFieldTextDidChangeNotification object:textField];

    }];

    [controller addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

        textField.placeholder = @"密码";

        textField.secureTextEntry = YES;

    }];

        /*当确定按钮按下时  读取*/

    okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

    [[NSNotificationCenter defaultCenter]removeObserver:self];

    }];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        [self.navigationController popToRootViewControllerAnimated:YES];

    }];

    [controller addAction:okAction];

    [controller addAction:cancelAction];

    okAction.enabled = NO;

    [self presentViewController:controller animated:YES completion:nil];

}

- (void)usernameChanged{

    okAction.enabled = controller.textFields.firstObject.text.length >= 3;

}


实现效果是这样的:

长度没有3位:


如果长度达到了3位 显示效果是这样的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值