Swift - 告警提示框(UIAlertController)的用法
自iOS8起,苹果就建议告警框使用UIAlertController来代替UIAlertView。下面总结了一些常见的用法:

|
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 UIKitclass 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 UIKitclass 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)的用法的更多相关文章
- 选择提示框UIAlertController 和网络状态判断AFNetworking
// 选择提示框 DownloadView *vc = [[DownloadView alloc] initWithFrame:CGRectMake(, , SCREEN_WIDTH, SCREEN_ ...
- iOS -iOS9中提示框(UIAlertController)的常见使用
iOS 8 之前提示框主要使用 UIAlertView和UIActionSheet:iOS 9 将UIAlertView和UIActionSheet合二为一为:UIAlertController . ...
- Swift - 警告提示框(UIAlertController)的用法
import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...
- Swift_IOS之提示框UIAlertController
import UIKit class ViewController: UIViewController ,UIActionSheetDelegate{ @IBAction func btn1(_ se ...
- js消息提示框插件-----toastr用法
(本文系转载) 因为个人项目中有一个提交表单成功弹出框的需求,从网上找了一些资料,发现toastr这个插件的样式还是不错的.所以也给大家推荐下,但是网上的使用资料不是很详细,所以整理了一下,希望能给 ...
- swift - UIAlertController 的用法
ios 8 以后苹果官方建议使用UIAlertController这个类,所以专门去网上找资料,了解了下用法, 1.创建一个alertController let alertController = ...
- 19. UIAlertController 提示框获取文本内容,打印控制台上
1.首先定义一个全局字符串变量,方便接收获取的文本内容 2. -(void)viewDidAppear:(BOOL)animated{ UIAlertController * alert = [UIA ...
- 提示框(UIAlertController)的使用。
添加出现在屏幕中间的提示框(也就是之前的UIAlertView): UIAlertController * av = [UIAlertController alertControllerWithTit ...
- WKWebView不显示提示框(Swift)
使用WKWebView的时候会出现明明自己做的一些页面有提示框, 为什么使用别人的页面提示框总是不显示, 其实很大部分原因是因为该提示框是通过JS调用的, 需要实现WKUIDelegate来进行监听 ...
随机推荐
- 02-UIKit控件、MVC
目录: 一.控件使用 二.动态类型和静态类型 三.MVC 四.UIAlertView对话框 回到顶部 一.控件使用 1 事件源,事件处理方法有一个参数传进来,那个参数就是触发这个事件的时间源. UIS ...
- VC6.0 导入资源崩溃
等我以后挣钱了一定买正版! 最近学习Win32编程,为了锻炼自己,在网上下载了一个VC6.0作为开发工具,应该是兼容性的问题吧,VC6 经常闹毛病. 今天导入资源的时候VC6出现崩溃的现象. 马上寻求 ...
- Qt属性系统
The Property System Qt提供一个类似于其他编译器供应商提供的精致的属性系统.然而,作为一个编译器和平台独立的库,Qt并不依赖于非标准编译器特性,如__property 或 [pro ...
- wince下GetManifestResourceStream得到的Stream是null的解决
问题的引入 在编程过程中遇到下面这样一个问题: 有这样一个方法: public static AlphaImage CreateFromResource(string imageResourceNam ...
- Solr搜索结果说明
在admin页面,输入相关内容后,会返回xml格式的内容.说明如下: [html] view plaincopy <?xml version="1.0" encoding=& ...
- <Win32_18>平滑的人物走动 —— 解决闪屏
今天咋一看,发现很久没写博客了 的确,开学之后,写博客的时间越来越少了…… 今天来做一个比较实用的小应用——平滑的人物走动,同时解决常见的闪屏问题.实现透明位图 这些技术在游戏开发中是很常见的 --- ...
- 管理启示,不起毛的鹦鹉——leo锦书54
下面一个很长的故事后,我真的很期待明确: 一个人去买鹦鹉,看到一仅仅鹦鹉前标:此鹦鹉会两国语言,售价二百元.还有一仅仅鹦鹉前则标道:此鹦鹉会四门语言.售价四百元.该买那仅仅呢?两仅仅都毛色鲜亮, ...
- PHP - 使用pear的HTTP_Upload包进行上传
前台代码: <html> <head> <title>上传文件</title> </head> <body> <form ...
- 嵌入式环境:CentOS下添加用户并且让用户获得root权限
CentOS下添加用户并且让用户获得root权限 http://www.centoscn.com/CentOS/config/2014/0810/3471.html 1.添加用户,首先用adduser ...
- hdu4708 Rotation Lock Puzzle
Rotation Lock Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...