• iOS 关于UIAlertController常见使用方法


    Step--1:警告框

     1.代码

     /*1.创建提示窗口

        参数1;Title:标题

        参数1;message:提示内容

        参数1;Style:风格 UIAlertControllerStyleAlert 警告框

         */

     UIAlertController*alertcontroller = [UIAlertControlleralertControllerWithTitle:@"提示"message:@"请按提示操作!"preferredStyle:UIAlertControllerStyleAlert];

     //实例化取消按钮

     UIAlertAction*cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction* _Nonnullaction) {

     //对按钮应用标准样式:  UIAlertActionStyleDefault

     //对按钮应用取消样式:  UIAlertActionStyleCancel

     //对按钮应用警示性的样式:UIAlertActionStyleDestructive

     NSLog(@"点击了取消...");

        }];

     //实例化确定按钮

     UIAlertAction*sureAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction* _Nonnullaction) {

     NSLog(@"点击了确定...");

        }];

    [alertcontroller addAction:cancelAction];

    [alertcontroller addAction:sureAction];

     //弹出提示框

        [selfpresentViewController:alertcontroller animated:YEScompletion:nil];

    2.样式

    Step--1:操作表

     1.代码

     /*1.创建提示窗口

         参数1;Title:标题

         参数1;message:提示内容

         参数1;Style:风格 UIAlertControllerStyleActionSheet 操作表

         */

     UIAlertController*alertcontroller = [UIAlertControlleralertControllerWithTitle:@"提示"message:@"请按提示操作!"preferredStyle:UIAlertControllerStyleActionSheet];

     //实例化取消按钮

     UIAlertAction*cancelAction = [UIAlertActionactionWithTitle:@"阿里"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction* _Nonnullaction) {

     //对按钮应用标准样式:  UIAlertActionStyleDefault

     //对按钮应用取消样式:  UIAlertActionStyleCancel

     //对按钮应用警示性的样式:UIAlertActionStyleDestructive

     NSLog(@"点击了取消...");

        }];

     //实例化确定按钮

     UIAlertAction*sureAction = [UIAlertActionactionWithTitle:@"腾讯"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction* _Nonnullaction) {

     NSLog(@"点击了确定...");

        }];

     //实例化其他按钮

     UIAlertAction*otherAction = [UIAlertActionactionWithTitle:@"其他"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction* _Nonnullaction) {

     NSLog(@"点击了其他...");

        }];

    [alertcontroller addAction:cancelAction];

    [alertcontroller addAction:sureAction];

    [alertcontroller addAction:otherAction];

     //弹出提示框

        [selfpresentViewController:alertcontroller animated:YEScompletion:nil];

        2.样式

    Step--1:带输入框的警告框

      1.代码

     UIAlertController*alertcontroller = [UIAlertControlleralertControllerWithTitle:@"提示"message:@"请按提示操作!"preferredStyle:UIAlertControllerStyleAlert];

     __weak__typeof(&*self)weakSelf = self;//block 中防止循环引用

    [alertcontroller addTextFieldWithConfigurationHandler:^(UITextField* _NonnulltextField) {

     //textField 属性设置

             textField.placeholder= @"请输入用户名";

     //通过通知监听textField的改变

            [[NSNotificationCenterdefaultCenter] addObserver:weakSelf selector:@selector(textfildChange0:) name:UITextFieldTextDidChangeNotificationobject:textField];

     //通过SEL监听textField的改变

    [textField addTarget:selfaction:@selector(textfildChange1:) forControlEvents:UIControlEventEditingChanged];

        }];

     //实例化取消按钮

     UIAlertAction*cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction* _Nonnullaction) {

     NSLog(@"点击了取消...");

        }];

     //实例化确定按钮

     UIAlertAction*sureAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction* _Nonnullaction) {

            [[NSNotificationCenterdefaultCenter] removeObserver:selfname:UITextFieldTextDidChangeNotificationobject:nil];

     //可以在这里获取textFields的信息

     NSString*userName = alertcontroller.textFields.lastObject.text;

     NSLog(@"该用户名:%@",userName);

        }];

    [alertcontroller addAction:cancelAction];

    [alertcontroller addAction:sureAction];

     //弹出提示框

          [selfpresentViewController:alertcontroller animated:YEScompletion:nil];

    - (void)textfildChange0:(NSNotification*)notification{

     UITextField*textfield = notification.object;

     NSLog(@"notification.userInfo %@",textfield.text);

    }

    - (void)textfildChange1:(UITextField*)textfield{

     NSLog(@"textfield %@",textfield.text);

    }

      2.样式

  • 相关阅读:
    KKFileView在线预览禁用复制右键图片保存等操作
    STM32的外部SRAM
    MySQL CREATE TABLE 简单设计模板交流
    [动态规划] (十四) 简单多状态 LeetCode LCR 091.粉刷房子
    暑期JAVA学习(45)动态代理
    Docker+consul实现容器服务的发现和更新
    快手小范围内测将“商城”独立,并放在顶部tab位置
    soildwork2022怎么恢复软件界面的默认设置?
    知识图谱实体对齐1:基于平移(translation)嵌入的方法
    规则引擎调研及初步使用
  • 原文地址:https://blog.csdn.net/qq_34491373/article/details/126420097