IOS-UIAlertController简单使用-Swift

news/2024/7/20 20:04:55 标签: swift, 开发语言, ios, 对话框

UIAlertControlle时IOS的对话框控制器(警报控制器),简单使用方法如下:
步骤都一样,先是创建UIAlertController,然后创建UIAlertAction,再将UIAlertAction添加到UIAlertController中,最后显示对话框

文本对话框

swift">		//创建控制器
        let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
        //设置action
        let okAction = UIAlertAction(title: "OK", style: .default){
            (action) in
            print("click OK")
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        //添加action
        alertController.addAction(okAction)
        alertController.addAction(cancelAction)
        //显示对话框
        present(alertController, animated: true, completion: nil)

效果如图:
在这里插入图片描述

带输入框的对话框

swift">		//创建控制器
        let alertController = UIAlertController(title: "Enter Text", message: nil, preferredStyle: .alert)
        //设置输入框
        alertController.addTextField { (textField) in
            textField.placeholder = "Enter text"
        }
        //设置action
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        let submitAction = UIAlertAction(title: "Submit", style: .default) { (action) in
            if let text = alertController.textFields?.first?.text {
                print("Entered text: \(text)")
            }
        }
        //添加action
        alertController.addAction(cancelAction)
        alertController.addAction(submitAction)
        //显示对话框
        present(alertController, animated: true, completion: nil)

效果如图:
在这里插入图片描述

底部选择对话框

注意preferredStyle为.actionSheet

swift">		//创建控制器
        let alertController = UIAlertController(title: "Choose Option", message: nil, preferredStyle: .actionSheet)
        //设置action
        let option1Action = UIAlertAction(title: "Option 1", style: .default) { (action) in
            print("Option 1 selected")
        }
        let option2Action = UIAlertAction(title: "Option 2", style: .default) { (action) in
            print("Option 2 selected")
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        //添加action
        alertController.addAction(option1Action)
        alertController.addAction(option2Action)
        alertController.addAction(cancelAction)
        //显示对话框
        present(alertController, animated: true, completion: nil)

效果如图:
在这里插入图片描述


http://www.niftyadmin.cn/n/5333027.html

相关文章

C++I/O流——(4)格式化输入/输出(第二节)

归纳编程学习的感悟, 记录奋斗路上的点滴, 希望能帮到一样刻苦的你! 如有不足欢迎指正! 共同学习交流! 🌎欢迎各位→点赞 👍 收藏⭐ 留言​📝 含泪播种的人一定能含笑收获&#xff…

VUE工程化--vue组件注册

组件注册的两种方式: 1. 局部注册:只能在注册的组件内使用 2. 全局注册:所有组件内都能使用 局部注册步骤: 1、导入 import MyHeader from "./components/myHeader.vue"; import MyMain from "./components/myMa…

23.实战演练--个人主页

<?xml version"1.0" encoding"utf-8"?> <manifest xmlns:android"http://schemas.android.com/apk/res/android"xmlns:tools"http://schemas.android.com/tools"><applicationandroid:allowBackup"true"an…

如何提高Linux系统安全性

Linux 是一种开源操作系统&#xff0c;被世界各地的个人、企业和政府广泛使用。然而&#xff0c;与任何其他操作系统一样&#xff08;甚至是更&#xff09;&#xff0c;Linux 也容易受到安全威胁&#xff0c;因此采取措施提高系统的安全性非常重要。 一、定期更新系统 提高 Li…

实现Jenkins Master和Agent的动态绑定

大家好。我是咕噜美乐蒂&#xff0c;很高兴又见面啦。那我们今天就来探讨一下如何实现实现Jenkins Master和Agent的动态绑定吧&#xff0c;下面来一起了解一下。 Jenkins是一个流行的开源持续集成和交付工具&#xff0c;它允许开发人员自动构建、测试和部署应用程序。在Jenkin…

Kotlin特性学习笔记

1,关键字by修饰类,表示类委托 interface Animation{fun eat() }//动态代理 class Dog:Animation{override fun eat() {println("dog eat oligarch")} }class DogProxy:Animation by Dog(){} 2,关键字by修饰变量,实现属性委托 var name:String by NameDelegate()…

YOLOv8在NX上的tensorrt的加速部署(60帧率)

所需环境 所有过程均可以参考本人所写的文章 (1)虚拟环境工具 MInforge3-Linux-aarch64 Jetson 平台都是RAM架构,平常的conda都是基于X86架构平台的。环境搭建参考文章 (2)YOLOv8_ros代码,采用自己创建的yolov_ros代码。yolov8_ros参考文章 (3)jetpack 环境(本篇文章…

003-90-15【SparkSQLDFDS】慈航寺庙山脚下八卦田旁油菜花海深处人家王大爷家女儿用GPT学习DataSet的基本操作

003-90-14【SparkSQL&DF&DS】慈航寺庙山脚下八卦田旁油菜花海深处人家王大爷家女儿用GPT学习DataSet的基本操作 【SparkSQL&DF&DS】Dataset 的创建和使用 【SparkSQL&DF&DS】2,Dataset 的创建和使用1&#xff0c; 创建2, show3, map4, as5, select6 f…