Swift完成UIAlertController的调用

iOS8中的UIAlertView和UIActionSheet已经都被UIAlertViewController代替了,所以,本篇blog就来探讨下如何用swift生成提示框。

我们先来看一下Apple的UIAlertController的文档:

import Foundation

import UIKit

//

// UIAlertController.h

// UIKit

//

// Copyright (c) 2014 Apple Inc. All rights reserved.

//

@availability(iOS, introduced=8.0)

enum UIAlertActionStyle : Int {

case Default

case Cancel

case Destructive

}

@availability(iOS, introduced=8.0)

enum UIAlertControllerStyle : Int {

case ActionSheet

case Alert

}

@availability(iOS, introduced=8.0)

class UIAlertAction : NSObject, NSCopying {

convenience init(title: String, style: UIAlertActionStyle, handler: ((UIAlertAction!) -> Void)!)

var title: String { get }

var style: UIAlertActionStyle { get }

var enabled: Bool

}

@availability(iOS, introduced=8.0)

class UIAlertController : UIViewController {

convenience init(title: String?, message: String?, preferredStyle: UIAlertControllerStyle)

func addAction(action: UIAlertAction)

var actions: [AnyObject] { get }

func addTextFieldWithConfigurationHandler(configurationHandler: ((UITextField!) -> Void)!)

var textFields: [AnyObject]? { get }

var title: String?

var message: String?

var preferredStyle: UIAlertControllerStyle { get }

}

我们可以看到UIAlertController的style有两个,一个是ActionSheet,一个是Alert,而AlertActionStyle有3个: Default,Cancel, Destructive;所以我们新建Alert时可以这样:

var alert: UIAlertController = UIAlertController(title:nil, message:"您输入的电话号码有误,请检查后重新输入",

preferredStyle:UIAlertControllerStyle.Alert)

或者

var alert: UIAlertController = UIAlertController(title: nil, message:"test", preferredStyle: UIAlertControllerStyle.ActionSheet)

接下来我们来给Alert添加action,从文档中可以看到AlertAction有init函数,

我们来新建3个actions

var saveAction = UIAlertAction(title: "Save", style: .Default, handler:{

(alerts: UIAlertAction!) -> Void in

println("File saved")

})

var deleteAction = UIAlertAction(title: "Delete", style: .Default, handler:{

(alerts: UIAlertAction!) -> Void in

println("File delete")

})

var cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler:{

(alerts: UIAlertAction!) -> Void in

println("Cancelled")

})注意到handler中用到了一个closure

然后给我们的alertcontroller添加actions,并把它显示出来

alert.addAction(saveAction)

alert.addAction(deleteAction)

alert.addAction(cancelAction)

self.presentViewController(alert, animated: true, completion: nil)

我们也可以这样添加action

alert.addAction(UIAlertAction(title: "确定", style: .Destructive, handler: {

action in switch action.style{

case .Default:

println("ok")

case .Cancel:

println("cancel")

case .Destructive:

println("Destructive")

}

}

))接下来运行一下看看我们的alertController是什么样子的吧。

Tips:

如果style是cancel 那么字体会变粗;如果是destructive,字体会显示红色。

http://www.dengb.com/Javabc/957757.htmlwww.dengb.comtruehttp://www.dengb.com/Javabc/957757.htmlTechArticleSwift完成UIAlertController的调用 iOS8中的UIAlertView和UIActionSheet已经都被UIAlertViewController代替了,所以,本篇blog就来探讨下如何用swift生成提示框...

swift怎么调用Java,Swift完成UIAlertController的调用相关推荐

  1. jni调用java类_JNI之C++调用Java类 —— java.lang.String

    JNI之C++调用Java类 -- java.lang.String 为什么要用C++调用Java类?很难回答,写着文章只是觉得JNI很有意思.于是开始编写一段使用VC++在Windows系统里调用j ...

  2. python调用java文件_Python程序中调用Java代码的实践

    1.环境准备 windows 7(64位)+JDK(64位)+Python(64位)+eclipse+pycharm 还需要一个作为桥梁的工具包,jpype1,这个工具包可以启动jvm,使java代码 ...

  3. scala调用java库_从scala调用java时的java.lang.IllegalAccessError – solutions / workarounds?...

    我正在使用 java线性代数库(ojalgo 32.0)进行scala项目,我遇到了一个 奇怪的问题.我使用的每种ojalgo方法都很好(例如矩阵 和逐元素乘法,逆矩阵和随机矩阵 除了用于获得矩阵尺寸 ...

  4. python调用java的jar包_python调用java的jar包报错127

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 最近在弄python需要调用到Java的jar包,按照网上的教程走,最后总是报错No matching overloads found for [init ...

  5. ie 调用java的时候报错,调用javabean的非常郁闷的异常。

    当前位置:我的异常网» Java Web开发 » 调用javabean的非常郁闷的异常. 调用javabean的非常郁闷的异常. www.myexceptions.net  网友分享于:2013-09 ...

  6. python如何调用java写的接口_Python 调用翻译接口

    最近在读 gartner 的一些文档,然后,,,全英文 然后看的我头昏脑涨....重点是效率低下...那我就想打开了我的有道,准备看到不认得的就鼠标悬浮一会,然后等反馈.. 可是,,这也太难了吧... ...

  7. 如何调用java的包_jsp如何调用java包

    我写的java代码如下:packagesy;publicclassshiyan{publicStringget(Stringt){returnt;}}jsp代码如下: 我写的java代码如下: pac ...

  8. android调用java接口_java/Android 接口调用的几种写法

    虽然Handler用的地方比较普遍,但是接口也有他的独特之处,比较直观,然后降低了耦合性 如有一接口,需要将数据传给使用的activity中,接口如下 public interfacePushValu ...

  9. 基于 Android NDK 的学习之旅----- C调用Java

    2019独角兽企业重金招聘Python工程师标准>>> 基于 Android NDK 的学习之旅----- C调用Java 许多成熟的C引擎要移植到Android 平台上使用 , 一 ...

最新文章

  1. 跨时钟域设置set_false_path的问题
  2. BIO与NIO、AIO的区别(这个容易理解)
  3. python序列类型-Python数值类型和序列类型
  4. 色诱社报道:昨日,腾讯公司公布了2009年发展策划
  5. linux星期六字符,linux shell系列10 判断某个月中的星期六和星期天
  6. 浅谈C#取消令牌CancellationTokenSource
  7. Linux创建、删除文件和文件夹命令
  8. 洛谷 P1031 均分纸牌【交叉模拟】
  9. 学习C++项目—— 搭建多线程网络服务框架,性能测试(并发性能测试,业务性能测试,客户端响应时间测试,网络带宽测试)
  10. MySQL5.7安装手册
  11. [MATLBA]imresize函数的用法
  12. 我的 Java 自学之路
  13. 重磅!公安部再度认可电子签名、电子印章法律效力!
  14. js中html5修改字体大小,jquery设置字体大小插件
  15. 03-Kubernetes中的Deployment
  16. 【RabbitMQ】基础四:路由模式(Routing)
  17. 3D建模贴图是个啥?先薅羊毛再说!(附1000张高清3D贴图素材)
  18. linux usb有线网卡驱动_有线网卡Linux驱动安装小记
  19. php 处理png图片白色背景色改为透明色
  20. 区块链系列课第二讲区块链的核心优势

热门文章

  1. 可扩展的TextView,ExpandableTextView与Scroller类的使用
  2. 论文学习18-Relation extraction and the influence of automatic named-entity recognition(联合实体关系抽取模型,2007)
  3. 淘淘相关工具类【json,httpClient,id,FTP,exception,cookie(包括共享cookie的设置等)】
  4. 去月球“你知道戴维会变身成哪种动物吗?”
  5. Unity3d—做一个年月日选择器(Scroll Rect拖动效果优化)— 无限滚动 + 锁定元素...
  6. mysql递归层次查询
  7. 《C++ Primer》读书笔记 第三章
  8. linux的基础知识——捕捉SIGCHLD、信号传参,中断系统调用
  9. 计算机网络(十九)-IEEE802.11无线局域网
  10. 第二章 物理层 1 物理层的基本概念 [计算机网络笔记]