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. c++读取ini的Section节名

    // ConsoleApplication1.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include "iostream&q ...

  2. IDEA常见错误解决

    tomcat控制台乱码 在tomcat的edit configurations里加入参数:-Dfile.encoding=UTF-8   导入的项目在重写时报 @Override is not all ...

  3. 20155231 2016-2017-2 《Java程序设计》第4周学习总结

    20155231 2016-2017-2 <Java程序设计>第4周学习总结 教材学习内容总结 学习目标 理解封装.继承.多态的关系 理解抽象类与接口的区别 掌握S.O.L.I.D原则 了 ...

  4. 20155305《信息安全系统设计基础》10月18日课堂 fork,exic,wait

    20155305<信息安全系统设计基础>10月18日课堂 fork,exic,wait fork()函数 1.fork函数作用 一般来讲, 我们编写1个普通的c程序, 运行这个程序直到程序 ...

  5. Spring学习(十三)-----Spring 表达式语言(Spring EL)

    本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.SpEL是类似于OGNL和JSF EL的表达式语言,能够在运行时构建复杂 ...

  6. python-全栈开发-前方高能-内置函数

    python_day_14 13. 前方高能-内置函数 ⼀. 本节主要内容: 1. 内置函数 什么是内置函数? 就是python给你提供的. 拿来直接⽤的函数, 比如print., input等等. ...

  7. 「Leetcode」14. Longest Common Prefix(Java)

    分析 与其说是算法题,不如说是语言特性题. 这题要是对Java的String相关函数掌握的比较熟练,写起来的速度(各种意义上)就会很快. 大致的思路都是一致的,差不到哪里去,无非是枚举长度.值得一提的 ...

  8. uvaoj 156Ananagrams(map和vector组合使用)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. php 操作 oracle lob 数据

    http://www.oracle.com/technetwork/articles/fuecks-lobs-095315.html Working with LOBs in Oracle and P ...

  10. pyquery详细用法

    python爬虫之PyQuery的基本使用   PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择,PyQue ...