自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. uva 10330 - Power Transmission(网络流)

    uva 10330 - Power Transmission 题目大意:最大流问题. 解题思路:増广路算法. #include <stdio.h> #include <string. ...

  2. CSS3_3D效果(IE10_火狐_谷歌)

    好久没写博客了,看了下记录,上次最后写的最后一篇已经是8月1号了,最近有些小东西整理下,当巩固吧 废话少说,直奔本文主题 css3提供了很多新鲜好玩的东西,transform就是其中一个,可以进行 2 ...

  3. Qt序列化格式分析(qint,QString)(非常简单好用)

    最近项目需要进行QT开发环境下对传输对象进行序列化与反序列化处理,对基本类型的处理在使用QT默认的序列化方式还是完全手工序列化这两种方式之间有些犹疑不定,边想了解下QT默认序列化基本类型的格式,项目中 ...

  4. lokijs

    http://lokijs.org/#/ 500,000+ 1.1M ops/s. A fast, in-memory document-oriented datastore for node.js, ...

  5. Android项目实战手机安全卫士(02)

    目录 项目结构图 源代码 运行结果 项目源代码 项目结构图 源代码 清单 01.  SplashActivity.java package com.coderdream.mobilesafe.acti ...

  6. tcp/ip协议listen函数中backlog參数的含义

    listen函数的定义例如以下所看到的: #include <sys/socket.h> int accept(int sockfd, struct sockaddr * restrict ...

  7. 收货MIGO

    FUNCTION zrfc_mm003. *"---------------------------------------------------------------------- * ...

  8. 解决Xcode 7编译错误:does not contain bitcode

    连接地址:http://jingyan.baidu.com/article/8065f87f96cf462331249801.html 好不容易更新到Xcode 7.0.1,重新编译代码,报错: do ...

  9. 让office2003和office2010共存的方法【转】

    前段时间由于工作需要安装office2010,每次打开word都会弹出安装配置界面,反之亦然.于是我在网上找了不少资料.也试了不少方法,终于试用了以下方法得以解决,以下来源于网络. 电脑上同时安装了O ...

  10. CCNP交换实验(7) -- NAT

    1.静态NAT2.动态NAT3.复用内部全局地址的NAT(PAT) enableconf tno ip do loenable pass ciscoline con 0logg syncexec-t ...