自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 {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
     
    override func viewDidAppear(animated: Bool){
        super.viewDidAppear(animated)
         
        let alertController = UIAlertController(title: "系统提示",
            message: "您确定要离开hangge.com吗?", preferredStyle: UIAlertControllerStyle.Alert)
        let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
        let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Default,
            handler: {
                action in
                print("点击了确定")
        })
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }
     
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

2,除了弹出,还可以使用从底部向上滑出的样式

(注意:如果上拉菜单中有“取消”按钮的话,那么它永远都会出现在菜单的底部,不管添加的次序是如何)

 
1
2
3
4
5
6
7
8
9
var alertController = UIAlertController(title: "保存或删除数据", message: "删除数据将不可恢复",
    preferredStyle: UIAlertControllerStyle.ActionSheet)
var cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
var deleteAction = UIAlertAction(title: "删除", style: UIAlertActionStyle.Destructive, handler: nil)
var archiveAction = UIAlertAction(title: "保存", style: UIAlertActionStyle.Default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(deleteAction)
alertController.addAction(archiveAction)
self.presentViewController(alertController, animated: true, completion: nil)

3,按钮使用“告警”样式(文字颜色变红,用来来警示用户)

  
1
var okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Destructive, handler: nil)

4,添加任意数量文本输入框(比如可以用来实现个登陆框)

  
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
29
30
31
32
33
34
35
36
37
38
import UIKit
 
class ViewController: UIViewController ,UIActionSheetDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
     
    override func viewDidAppear(animated: Bool){
        super.viewDidAppear(animated)
         
        let alertController = UIAlertController(title: "系统登录",
            message: "请输入用户名和密码", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addTextFieldWithConfigurationHandler {
            (textField: UITextField!) -> Void in
            textField.placeholder = "用户名"
        }
        alertController.addTextFieldWithConfigurationHandler {
            (textField: UITextField!) -> Void in
            textField.placeholder = "密码"
            textField.secureTextEntry = true
        }
        let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
        let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Default,
            handler: {
                action in
                let login = alertController.textFields!.first! as UITextField
                let password = alertController.textFields!.last! as UITextField
                print("用户名:\(login.text) 密码:\(password.text)")
        })
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    }
     
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

5,使用代码移除提示框

1
self.presentedViewController?.dismissViewControllerAnimated(false, completion: nil)

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

  1. 选择提示框UIAlertController 和网络状态判断AFNetworking

    // 选择提示框 DownloadView *vc = [[DownloadView alloc] initWithFrame:CGRectMake(, , SCREEN_WIDTH, SCREEN_ ...

  2. iOS -iOS9中提示框(UIAlertController)的常见使用

    iOS 8 之前提示框主要使用 UIAlertView和UIActionSheet:iOS 9 将UIAlertView和UIActionSheet合二为一为:UIAlertController . ...

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

    import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...

  4. Swift_IOS之提示框UIAlertController

    import UIKit class ViewController: UIViewController ,UIActionSheetDelegate{ @IBAction func btn1(_ se ...

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

     (本文系转载) 因为个人项目中有一个提交表单成功弹出框的需求,从网上找了一些资料,发现toastr这个插件的样式还是不错的.所以也给大家推荐下,但是网上的使用资料不是很详细,所以整理了一下,希望能给 ...

  6. swift - UIAlertController 的用法

    ios 8 以后苹果官方建议使用UIAlertController这个类,所以专门去网上找资料,了解了下用法, 1.创建一个alertController let alertController = ...

  7. 19. UIAlertController 提示框获取文本内容,打印控制台上

    1.首先定义一个全局字符串变量,方便接收获取的文本内容 2. -(void)viewDidAppear:(BOOL)animated{ UIAlertController * alert = [UIA ...

  8. 提示框(UIAlertController)的使用。

    添加出现在屏幕中间的提示框(也就是之前的UIAlertView): UIAlertController * av = [UIAlertController alertControllerWithTit ...

  9. WKWebView不显示提示框(Swift)

    使用WKWebView的时候会出现明明自己做的一些页面有提示框, 为什么使用别人的页面提示框总是不显示, 其实很大部分原因是因为该提示框是通过JS调用的, 需要实现WKUIDelegate来进行监听 ...

随机推荐

  1. C# 窗体在线2,8,16进制转换以及,在线更新时间

    class Program { static void Main(string[] args) { //十进制转二进制 Console.WriteLine(, )); //十进制转八进制 Consol ...

  2. [转]mysql慢查询日志

    原文链接:http://www.cnblogs.com/zhangjing0502/archive/2012/07/30/2615570.html 参考博文:http://blog.chinaunix ...

  3. 17-UIKit(UIView的动画)

    2. UIView的动画 UIView类本身具有动画的功能 2.1 概念 由UI对底层Core Animation框架的封装 可以轻松简单的实现动画效果 2.2 两种使用方式 1> Block ...

  4. 一道经典的C++结构体的题目

    题目描述: 有10个学生,每个学生的数据包括学号.姓名.英语.数学.物理三门课的成绩,从键盘输入10个学生数据,要求打印出3门课程的总平均成绩,以及最高分的学生的数据(包括学号,姓名,3门课的平均成绩 ...

  5. crm高速开发之QueryExpression

    /* 创建者:菜刀居士的博客  * 创建日期:2014年07月06号  */ namespace Net.CRM.OrganizationService {     using System;     ...

  6. zepto.js介绍(持续更新)

    前言: zepto是一个简化版的jQuery,主要针对移动端开发. 简化了jQuery里很多的浏览器兼容性代码,jQuery的很多方法都被拿掉了(eg:slideUp). WP设备兼容性很差. 官方链 ...

  7. 更改ORACLE 用户的 expired状态

    oracle中, 经常用户的状态会变成locked, expired 等状态, 这种情况下怎么处理呢? 首先, 如果是locked状态还好办, DBA直接执行alter user scott acco ...

  8. Linux权限操作 [转]

    Linux权限操作 本文内容来自<鸟哥linux私房菜>读后个人做的笔记,该书实为学习linux的很好入门教材 一.文件属性 ls ls -al列出所有的档案属性 ls是List的意思 档 ...

  9. 超详细SDK Hello World

    Windows应用程序的基本运行机制与HelloWin程序详细解 总的来说最基本的Windows应用程序的运行执行顺序总是以如下的基本顺序执行的. 顺序结构: 调用WinMain函数开始执行--à定义 ...

  10. android Activity之间数据传递 Parcelable和Serializable接口的使用

    Activity之间传数据时,为了避免麻烦,往往会将一些值封装成对象,然后将整个对象传递过去.传对象的时候有两种情况,一种是实现Parcelable接口,一种是实现Serializable接口.0.解 ...