1.前言  

在前段时间手机QQ:升级iOS8.3后,发图就崩的情况,
就是因为iOS8更新UIAlertController后,仍然使用UIAlertview导致的
具体原因分析 这个可以看腾讯团队发出来的总结分享。
 
在Xcode头文件中苹果也明确给出用UIAlertController替代UIAlertview和UIActionSheet的标识
 
 
 
所以iOS8以后我们还是使用苹果推荐的UIAlertController吧(这货居然是一个ViewController。。)
 

2.如何使用UIAlertController  

2.2.第一种创建方式——默认提示框  

最原始的init一般不用这种,默认是上拉菜单样式

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
// 单击屏幕触发 //方式一
var alertVC = UIAlertController()
alertVC.title = "Title"
alertVC.message = "Hello,My name Saup" //因为UIAlertController是控制器,所以我们现在得改用控制器弹出
self.presentViewController(alertVC, animated: true, completion: nil) }

效果图1:

2.2.第二种创建方式——自定义提示框  

UIAlertControllerStyle
UIAlertControllerStyle.Alert        对话框样式
UIAlertControllerStyle.ActionSheet  上拉菜单样式
注意第三个参数,要确定您选择的是对话框样式还是上拉菜单样式。
 
 
UIAlertActionStyle
通过UIAlertActionStyle,可以选择如下三种动作样式:
常规(default)、取消(cancel)以及警示(destruective)。
UIAlertActionStyle.Default
UIAlertActionStyle.Cancel
UIAlertActionStyle.Destructive //“警告”样式会默认把按钮字体加红
 
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
// 单击屏幕触发 //方式二 //创建控制器
var alertVC = UIAlertController(title: "Title", message: "Please choose!", preferredStyle: UIAlertControllerStyle.ActionSheet)
//创建按钮
var acSure = UIAlertAction(title: "Sure", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in
print("click Sure")
} var acCancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (UIAlertAction) -> Void in
print("click Cancel")
} // var acDestuctive = UIAlertAction(title: "Destuctive", style: //UIAlertActionStyle.Destuctive) { (UIAlertAction) -> Void in
// print("click Destuctive")
// } alertVC.addAction(acSure)
alertVC.addAction(acCancel)
// alertVC.addAction(acDestuctive) //因为UIAlertController是控制器,所以我们现在得改用控制器弹出
self.presentViewController(alertVC, animated: true, completion: nil) }

效果图2:

2.3.第三种创建方式——文本对话框  

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
// 单击屏幕触发 //方式三 //创建控制器
var alertVC = UIAlertController(title: "TextFiled", message: "Please input!", preferredStyle: UIAlertControllerStyle.Alert) alertVC.addTextFieldWithConfigurationHandler { (tField:UITextField!) -> Void in tField.placeholder = "Account" } alertVC.addTextFieldWithConfigurationHandler {(textField:UITextField!) -> Void in
textField.placeholder = "Password"
textField.secureTextEntry = true;
} var acOK = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (alertAction:UIAlertAction!) -> Void in }
var acCancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (alertAction:UIAlertAction!) -> Void in
} acOK.enabled = false alertVC.addAction(acOK)
alertVC.addAction(acCancel) //因为UIAlertController是控制器,所以我们现在得改用控制器弹出
self.presentViewController(alertVC, animated: true, completion: nil)
}

效果图3:

作者: 清澈Saup
出处:http://www.cnblogs.com/qingche/
本文版权归作者和博客园共有,欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文连接。

iOS- Swift:如何使用iOS8中的UIAlertController的更多相关文章

  1. iOS 学习笔记 九 (2015.04.02)IOS8中使用UIAlertController创建警告窗口

    1.IOS8中使用UIAlertController创建警告窗口 #pragma mark - 只能在IOS8中使用的,警告窗口- (void)showOkayCancelAlert{    NSSt ...

  2. iOS8中的UIAlertController

    转:      iOS8推出了几个新的“controller”,主要是把类似之前的UIAlertView变成了UIAlertController,这不经意的改变,貌似把我之前理解的“controlle ...

  3. iOS开发手记-iOS8中使用定位服务解决方案

    问题描述: 在iOS8之前,app第一次开始定位服务时,系统会弹出一个提示框来让用户选择是否允许使用定位信息.但iOS8后,app将不会出现这个弹窗.第一次运行之后,在设置->隐私->定位 ...

  4. 在iOS 8中使用UIAlertController

    iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...

  5. 【iOS】swift-ObjectC 在iOS 8中使用UIAlertController

    iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...

  6. [iOS] 初探 iOS8 中的 Size Class

    本文转载至  http://www.itnose.net/detail/6112176.html   以前和安卓的同学聊天的时候,谈到适配一直是一个非常开心的话题,看到他们被各种屏幕适配折磨的欲仙欲死 ...

  7. iOS开发--Swift 如何完成工程中Swift和OC的混编桥接(Cocoapods同样适用)

    由于SDK现在大部分都是OC版本, 所以假如你是一名主要以Swift语言进行开发的开发者, 就要面临如何让OC和Swift兼容在一个工程中, 如果你没有进行过这样的操作, 会感觉异常的茫然, 不用担心 ...

  8. iOS8中的动态文本

    原文链接 : Swift Programming 101: Mastering Dynamic Type in iOS 8 原文作者 : Kevin McNeish Apple声称鼓励第三方App能够 ...

  9. [Swift]UIKit学习之警告框:UIAlertController和UIAlertView

    Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) T ...

随机推荐

  1. laravel5.5源码笔记(四、路由)

    今天这篇博文来探索一下laravel的路由.在第一篇讲laravel入口文件的博文里,我们就提到过laravel的路由是在application对象的初始化阶段,通过provider来加载的.这个路由 ...

  2. 嵌入式C语言自我修养 06:U-boot镜像自拷贝分析:section属性

    6.1 GNU C 的扩展关键字:attribute GNU C 增加一个 __atttribute__ 关键字用来声明一个函数.变量或类型的特殊属性.声明这个特殊属性有什么用呢?主要用途就是指导编译 ...

  3. 用函数指针将字符串 ”hello world“ 输出为 "world hello”

    /************************************************************************* > File Name: 指针数组2.c & ...

  4. golang基础--strcut结构体

    结构体struct类似python语言中的类class,结构体类的元素可以是一个变量,或者函数或者其它的类型,好比python的属性和方法. // struct结构体,类似python语言中的clas ...

  5. ubuntu配置机器学习环境(二) cuda 和cudnn 安装

    Nvidia CUDA Toolkit的安装(cuda) PS:特别推荐*.deb的方法,目前已提供离线版的deb文件,该方法比较简单,不需要切换到tty模式,因此不再提供原来的*.run安装方法,这 ...

  6. 20155330 实验三 敏捷开发与XP实践

    20155330 实验三 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验步骤 (一)敏捷开发与XP 软件工程是把系统的.有序的.可量化的方法应用到软件的开发.运营和维护上的过程. ...

  7. 20155337 2016-2017-2 《Java程序设计》第三周学习总结

    20155337 2016-2017-2 <Java程序设计>第死周学习总结 教材学习内容总结 第六章 •何谓继承: 面向对象中,为避免多个类间重复定义共同行为.(简单说就是将相同的程序代 ...

  8. IDEA 运行报错 failed to create a child event loop

    背景 在IDEA中写了测试代码,但是运行的时候一直提示 java.lang.IllegalStateException: failed to create a child event loop ... ...

  9. phpcms 的模板更换

    刚安装完成后的phpcms ,自带的默认模板样式一般,可以自己换模板 首先打开phpcms文件夹,按照下图路径打开 default是存模板的文件夹 在里面新建一个文件夹 在default中主要用到的是 ...

  10. C# VS,连接到oracle 报要升级到8.多少版本的错

    1:确定服务器的oracle版本 2:本地的客户端版本要和服务器一致 3:操作系统位数要一致