Swift - 警告提示框(UIAlertController)的用法

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        // 创建
        let alertController = UIAlertController(title: "提示", message: "你确定要离开?", preferredStyle:.Alert)

        // 设置2个UIAlertAction
        let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
        let okAction = UIAlertAction(title: "好的", style: .Default) { (UIAlertAction) in
            print("点击了好的")
        }

        // 添加
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)

        // 弹出
        self.presentViewController(alertController, animated: true, completion: nil)
    }
}

// 除了弹出,还可以使用底部向上滑出的样式
        // 注意:如果上拉菜单中有『取消』按钮的话,那么它永远都会出现在菜单的底部,不管添加的次序如何

        // 创建
        // preferredStyle 为 ActionSheet
        let alertController = UIAlertController(title: "保存或删除数据", message: "删除数据将不可恢复", preferredStyle:.ActionSheet)

        // 设置2个UIAlertAction
        let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
        let deleteAction = UIAlertAction(title: "删除", style: .Destructive, handler: nil)
        let saveAction = UIAlertAction(title: "保存", style: .Default, handler: nil)

        // 添加到UIAlertController
        alertController.addAction(cancelAction)
        alertController.addAction(saveAction)
        alertController.addAction(deleteAction)

        // 弹出
        self.presentViewController(alertController, animated: true, completion: nil)

/*
        添加任意数量的文本输入框(比如可以用来实现登录框)
         */

        let alertController = UIAlertController(title: "系统登录", message: "请输入用户名和密码", preferredStyle: UIAlertControllerStyle.Alert)

        alertController.addTextFieldWithConfigurationHandler { (textField:UITextField) in
            textField.placeholder = "用户名"
        }
        alertController.addTextFieldWithConfigurationHandler { (textField:UITextField) in
            textField.placeholder = "密码"
            textField.secureTextEntry = true
        }

        let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
        let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Default) { (UIAlertAction) in
            let login = alertController.textFields![0]
            let pwd = alertController.textFields![1]
            print("用户名:\(login.text) 密码:\(pwd.text)")
        }

        alertController.addAction(cancelAction)
        alertController.addAction(okAction)

        // 弹出
        self.presentViewController(alertController, animated: true, completion: nil)
时间: 2024-08-24 07:55:10

Swift - 警告提示框(UIAlertController)的用法的相关文章

Swift - 告警提示框(UIAlertController)的用法

自iOS8起,苹果就建议告警框使用UIAlertController来代替UIAlertView.下面总结了一些常见的用法: 1,简单的应用(同时按钮响应Handler使用闭包函数) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 import UIKit class ViewController: UIViewController ,UIActionSheetDelegate {     ov

警告提示框(AlertControl)

设置提示框的透明度 1 private void alertControl1_BeforeFormShow(object sender, 2 DevExpress.XtraBars.Alerter.AlertFormEventArgs e) 3 { 4 e.AlertForm.OpacityLevel = 1; //0为透明,1为不透明 5 } AutoFormDelay属性用来设置窗体延迟,默认7000ms. FormDisplaySpeed属性用来设置窗口显示速度 FormMaxCount数

js消息提示框插件-----toastr用法

(本文系转载) 因为个人项目中有一个提交表单成功弹出框的需求,从网上找了一些资料,发现toastr这个插件的样式还是不错的.所以也给大家推荐下,但是网上的使用资料不是很详细,所以整理了一下,希望能给大家带来帮助. toastr 官网http://codeseven.github.io/toastr/ 这个样式插件支持直接导cdn入链接,但是我尝试了一下cdn加载速度太慢,所以推荐直接下载文件后导入文件 个人演示地址,因为把js放在了头部所以加载时间可能有点长,而且我发现自己的服务器不是很稳定,所

swift - UIAlertController 的用法

ios 8 以后苹果官方建议使用UIAlertController这个类,所以专门去网上找资料,了解了下用法,自己写个代码,记录一下! 感谢航哥,转自:http://www.hangge.com/blog/cache/detail_651.html 1.创建一个alertView override func viewDidAppear(animated: Bool){ super.viewDidAppear(animated) let alertController = UIAlertContr

iOS:提示框(警告框)控件UIActionSheet的详解

提示框(警告框)控件2:UIActionSheet 功能:当点击按钮或标签等时,弹出一个提示框,显示必要的提示,然后通过添加的按钮完成需要的功能.它与导航栏类似,它继承自UIView. 风格类型: typedef NS_ENUM(NSInteger, UIActionSheetStyle) { UIActionSheetStyleAutomatic        = -1,       //iOS系统自动默认的风格 UIActionSheetStyleDefault          = UIB

WKWebView不显示提示框(Swift)

使用WKWebView的时候会出现明明自己做的一些页面有提示框, 为什么使用别人的页面提示框总是不显示, 其实很大部分原因是因为该提示框是通过JS调用的, 需要实现WKUIDelegate来进行监听 // MARK: - WKUIDelegate // 监听通过JS调用警告框 func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame:

iOS:提示框(警告框)控件UIAlertView的详解

提示框(警告框)控件:UIAlertView 功能:当点击按钮或标签等时,弹出一个提示框,显示必要的提示,然后通过添加的按钮完成需要的功能. 类型:typedef NS_ENUM(NSInteger, UIAlertViewStyle) { UIAlertViewStyleDefault = 0,                 //默认类型 UIAlertViewStyleSecureTextInput,          //安全密码的文本框输入类型 UIAlertViewStylePlai

js三种消息框总结-警告框、确认框、提示框

js消息框类别:警告框.确认框.提示框 警告框:alert("文本"); 确认框:confirm("文本"); 提示框:prompt("文本","默认值"); 一:confirm使用范例 <script type="text/javascript"> function test(){ var res = confirm("请选择"); if(res == true){ doc

提示窗UIAlertView与UIAlertController的用法(持续更新中)

一般在if判断中加入 1.第一种基础型 全都在xxx.m功能文件中编写 UIAlertView(基础版): 1 UIAlertView *WXinstall=[[UIAlertView alloc]initWithTitle:@"提示框" message:@"提示信息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];//一般在if判断中加