• 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.样式

  • 相关阅读:
    深度思考计算机网络面经之三
    无线WIFI接入FreeRadius进行认证——筑梦之路
    Clickhouse MAP类型
    vite+rollup
    ceph块存储在线扩容
    .NET Core 中插件式开发实现
    函数式编程
    自动化安装脚本(Ansible+shell)
    迅为龙芯2K1000开发板通过汇编控制GPIO
    C中结构体和C++类各自对象的大小——C++
  • 原文地址:https://blog.csdn.net/qq_34491373/article/details/126420097