zoukankan      html  css  js  c++  java
  • UIAlertController 弹框提醒

    传统的alertView

    - (void)alertView
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"你的操作时非法的,您要继续吗" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
        [alert show];
    }

    这里写图片描述

    传统的actionSheet

    - (void)actionSheet
    {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"警告:你的操作时非法的,您要继续吗" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定"
                                                  otherButtonTitles:@"关闭", nil];
        [sheet showInView:self.view];
     }

    这里写图片描述

    UIAlertController == UIAlertView + UIActionSheet

    - (void)alertController
    {
        // 危险操作:弹框提醒
        // 1.UIAlertView
        // 2.UIActionSheet
        // iOS8开始:UIAlertController == UIAlertView + UIActionSheet
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"你的操作时非法的,您要继续吗" preferredStyle:UIAlertControllerStyleAlert];
    
        // 添加按钮
        __weak typeof(alert) weakAlert = alert;
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            NSLog(@"点击了确定按钮--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            NSLog(@"点击了取消按钮");
        }]];
    
        [alert addAction:[UIAlertAction actionWithTitle:@"其它" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSLog(@"点击了其它按钮");
        }]];
    
        // 添加文本框
        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.textColor = [UIColor redColor];
            textField.text = @"123";
            [textField addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged];
            //        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(usernameDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
        }];
        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
            textField.secureTextEntry = YES;
            textField.text = @"123";
        }];
    
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    - (void)usernameDidChange:(UITextField *)username
    {
        NSLog(@"%@", username.text);
    }

    这里写图片描述这里写图片描述

    其他操作

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"你的操作时非法的,您要继续吗" preferredStyle:UIAlertControllerStyleActionSheet];
        // 设置popover指向的item
        alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;
    
        // 添加按钮
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            NSLog(@"点击了确定按钮");
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            NSLog(@"点击了取消按钮");
        }]];
    
        [self presentViewController:alert animated:YES completion:nil];
    
    }
    // UIAlertControllerStyleActionSheet的使用注意
    // 1.不能有文本框
    // 2.在iPad中,必须使用popover的形式展示
    
    // Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert
    // 只能在UIAlertControllerStyleAlert样式的view上添加文本框

    这里写图片描述

     
  • 相关阅读:
    React性能优化 渲染20000多条checkbox组件,点击选择checkbox组件的时候特别卡,解决办法
    react网页多语言(react-intl-universal)
    个人作品
    程序员和产品经理的那些事
    three.js 根据不同的图片做不同的动画特效 科技感十足
    互联网行业最佳产品开发流程 推荐!
    类似于YouTube、Medium、知乎等网站使用的进度条插件 NProgress
    js模拟点击下载文件到本地
    MySQL记录操作(增删改)
    MySQL表的操作
  • 原文地址:https://www.cnblogs.com/crash-wu/p/4932446.html
Copyright © 2011-2022 走看看