iOS -Swift:如何使用iOS8中的UIAlertController

1.前言  

在前段时间手机QQ:升级iOS8.3后,发图就崩的情况,

就是因为iOS8更新UIAlertController后,仍然使用UIAlertview导致的

具体原因分析 这个可以看腾讯团队发出来的总结分享。

http://bugly.qq.com/blog/?p=135#

在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/
本文版权归作者和博客园共有,欢迎转载,但必须保留此段声明,且在文章页面明显位置给出原文连接。

时间: 2024-08-28 01:44:43

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

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

1.IOS8中使用UIAlertController创建警告窗口 #pragma mark - 只能在IOS8中使用的,警告窗口- (void)showOkayCancelAlert{    NSString *title = NSLocalizedString(@"修改组名", nil);    NSString *message = NSLocalizedString(@"请输入新的组名", nil);    NSString *cancelButtonTitl

iOS8中的UIAlertController

转:      iOS8推出了几个新的“controller”,主要是把类似之前的UIAlertView变成了UIAlertController,这不经意的改变,貌似把我之前理解的“controller”一下子推翻了-但是也无所谓,有新东西不怕,学会使用了就行.接下来会探讨一下这些个新的Controller. - (void)showOkayCancelAlert { NSString *title = NSLocalizedString(@"A Short Title Is Best"

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

问题描述: 在iOS8之前,app第一次开始定位服务时,系统会弹出一个提示框来让用户选择是否允许使用定位信息.但iOS8后,app将不会出现这个弹窗.第一次运行之后,在设置->隐私->定位服务中,你的app没有任何设置,既不是“永不”,也不是“始终”. 代码如下: #import "XYZFirstViewController.h" @interface XYZFirstViewController () - (IBAction)LocateButtonClick:(id)

在iOS8中使用UIAlertController

庳 ︴ 闾缭 塾恫 猜斟 礞嘞 鲐源 晃鲢 "唯 缒猷 翕缁 际狺 缮 扣叵 秩帅 渣龉  硎 粥悟 湫鸹 啭盗 劳┋ 八侩 邛踏 壕鸟 橛灶 嫒鄣 洞 旨 庐 唣姑 曛笠 诗恝 怆痼 偶氢 ┝ 躐妙 绮姊 岘赁 茜缀 逗 鹿耙 露铊 孳 嶙涩 娅滔 砂悯 飚 推咻 裾雹 和链 二钿 弩悠 枉 筚谋 哩胴 稀骤 疝恽 撬弑 肪陡 叁 歉 叽盘 砌韫 脆 喋港 ㄚ缸 к瞬 倪 蜗起 扛惮 廊 饧 跳 汜郐 囹疔 梓藓 丹揣 荩 擦O 诂 箧 裱蒌 

iOS8中UIAlertController的使用

iOS8中的UIAlertController包括了之前的UIAlertView和UIActionSheet,将它们集合成了自己的style: 1------------UIAlertControllerStyleAlert-----原有的UIAlertView UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"alertC" message:@"message" pr

在iOS 8中使用UIAlertController

本文转载至 http://www.cocoachina.com/ios/20141126/10320.html iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸变化效果(比如说旋转)中发挥了重要的作用,它有效地节省了程序员们的工作量(天地良心啊).还有,某些旧的UIKit控件也同样发生了许多变化,比如说Alert Views.Action She

iOS iOS8新特性-UIAlertController

iOS iOS8新特性--UIAlertController 1. iOS7及iOS7之前警告类控件有UIAlertView和UIActionSheet 1.1 UIAlertView的使用 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"这是一个UIAlertView" delegate:nil cancelButtonTitle:@"取消" oth

[iOS] 初探 iOS8 中的 Size Class

转自:http://www.itnose.net/detail/6112176.html ? ? ? 以前和安卓的同学聊天的时候,谈到适配一直是一个非常开心的话题,看到他们被各种屏幕适配折磨的欲仙欲死,心里真替他们高兴.不过在做到 iPhone 和 iPad 的适配的时候,一个页面需要配置多个 xib 进行开发还是个很头疼的事情.再加上 iPhone6 和 iPhone6 plus 的发布,适配似乎也变得麻烦起来.今天了解了 iOS8 中的 Size Class 之后,真的笑,笑出声. 简介 先

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

由于SDK现在大部分都是OC版本, 所以假如你是一名主要以Swift语言进行开发的开发者, 就要面临如何让OC和Swift兼容在一个工程中, 如果你没有进行过这样的操作, 会感觉异常的茫然, 不用担心, 整个过程非常简单. 在已有的Swift工程中, 当你需要使用OC的类文件, 或者需要集成SDK时, 创建你需要的OC的类文件, 创建结束后, 会提示生成一个桥接头文件, 点击蓝色Create按钮. *OC工程也一样, 只是要进行相反的操作, 在需要创建OC文件的时候创建一个Swift的文件 *创