UIAlertController样式集合

1.默认样式

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"你好" message:@"这个是UIAlertController的默认样式" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消");
    }];
    UIAlertAction *commitAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确认");
    }];
    [alertController addAction:cancleAction];
    [alertController addAction:commitAction];
    [self presentViewController:alertController animated:YES completion:nil];


2.输入框样式



UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"文本对话框" message:@"登陆和密码对话框" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"登陆";
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"密码";
        /**
         *  是否隐藏输入
         */
        textField.secureTextEntry = YES;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
    }];
    
    UIAlertAction *commitAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        /**
         *  这里面可以做一些与服务器端交互的事情
         */
        NSLog(@"%@\n", alertController.textFields.firstObject.text);
        NSLog(@"%@", alertController.textFields.lastObject.text);
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }];
    commitAction.enabled = NO;

    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"用户取消了登陆");
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }];
    /**
     *  添加交互按钮
     */
    [alertController addAction:cancleAction];
    [alertController addAction:commitAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
/**
 *  监听textField是否改变
 */
- (void)alertTextFieldDidChange:(NSNotification *)nc
{
    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
    if (alertController) {
        UITextField *userField = alertController.textFields.firstObject;
        UITextField *passWordField = alertController.textFields.lastObject;
        UIAlertAction *commitAction = alertController.actions.lastObject;
        commitAction.enabled = userField.text.length > 2 && passWordField.text.length > 6;
        NSLog(@"%d", commitAction.enabled);
    }
}


3.上拉菜单样式



UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"保存和删除数据" message:@"删除数据不可恢复" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];
    UIAlertAction *commitAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"确定");
    }];
    UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"保存");
    }];
    [alertController addAction:cancleAction];
    [alertController addAction:commitAction];
    [alertController addAction:saveAction];
    [self presentViewController:alertController animated:YES completion:nil];





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值