自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. iOS图片拉伸技巧-李明杰分享

    http://bbs.itcast.cn/thread-21436-1-1.html 本文目录 "一.iOS5.0之前------------------------------------ ...

  2. IntelliJ IDEA导出Java 可执行Jar包

    原文:IntelliJ IDEA导出Java 可执行Jar包 保证自己的Java代码是没有问题的,在IDEA里面是可以正常运行的,然后,按下面步骤: 打开File -> Project Stru ...

  3. 使用linq对字符串1,2,3,4,5,6,7,8,9,10求和

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SumI ...

  4. hdu 4940 Destroy Transportation system( 无源汇上下界网络流的可行流推断 )

    题意:有n个点和m条有向边构成的网络.每条边有两个花费: d:毁坏这条边的花费 b:重建一条双向边的花费 寻找这样两个点集,使得点集s到点集t满足 毁坏全部S到T的路径的费用和 > 毁坏全部T到 ...

  5. Lucene4.3入门

    辞职交接期间无聊看了一下搜索引擎,java社区比较火的当然是Lucene,想写一个简单的小例子,在网上找了些资料,不过都不是4.3的,自己看了一下. 下载地址:http://lucene.apache ...

  6. 总线接口与计算机通信(三)UART起止式异步通用串行数据总线

    串口简介 1. 什么是串口? 串口是计算机上一种非常通用的设备通信的协议.串口通信的概念非常简单,串口按位(bit) 发送和接收字节.尽管比按字节(byte)的并行通信慢,但是串口可以在使用一根线发送 ...

  7. js判断是否为pc端或移动端

    function IsPC() { var userAgentInfo = navigator.userAgent; var Agents = new Array("Android" ...

  8. “/” 应用程序中的服务器错误 - IIS 发布错误

    解决方法, 将bin目录下的全部文件复制到iis下

  9. Linux服务安全之TcpWrapper篇

    一.TcpWrapper的定义 任何以xinetd管理的服务都可以通过TcpWrapper来设置防火墙.简单地说,就是针对源IP或域进行允许或拒绝的设置,以决定该连接是否能够成功实现连接. 通过名称我 ...

  10. Python爬虫入门三之Urllib库的基本使用

    转自http://cuiqingcai.com/947.html 1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由 ...