iOS开发之UIAlertView与UIAlertController的详尽用法说明
本文将从四个方面对IOS开发中UIAlertView与UIAlertController的用法进行讲解:
一、UIAlertView与UIAlertController是什么东东?
二、我们为什么要用UIAlertView或UIAlertController?
三、如何使用UIAlertView和UIAlertController?
四、阅读提醒。
一、UIAlertView与UIAlertController是什么东东?
一句话,在所有移动应用中,出现的提示窗一般都由UIAlertView或UIAlertController来编写的,两者功能相同。
二、我们为什么要用UIAlertView或UIAlertController?
好的人机交互,可以提高用户体验,操作系统的人机交互功能是决定计算机系统“友善性”的一个重要因素。人机交互功能主要靠可输入输出的外部设备和相应的软件来完成。在传统时期,可供人机交互使用的设备主要有键盘显示、鼠标、各种模式识别设备等。与这些设备相应的软件就是操作系统提供人机交互功能的部分。早期的人机交互设施主要是键盘和显示器。操作员通过键盘打入命令,操作系统接到命令后立即执行并将结果通过显示器显示。打入的命令可以有不同方式,但每一条命令的解释是清楚的,唯一的。如今,在移动端我们所进行的所有操作均是在可触摸屏幕上进行的,为了更好的进行人机交互,工程师便把操作信息或提示信息显示到屏幕上,用户根据自身判断来决定所需点击的按钮。一句话,就是利用人机交互更好的提升用户的使用体验和产品设计的质量。
三、如何使用UIAlertView和UIAlertController?
1、UIAlertView的使用方法:
// Newly initialized alert view.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Edit" message:@"Please Modify the Info" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Sure", @"Other", nil]; // 为下面修改数据用
// This property is inherited from the UIView, You can use this property to distinguish when a AlertView has multiple view
alertView.tag = indexPath.row; // Adds a button to the receiver with the given title.
[alertView addButtonWithTitle:@"addBtn"]; /**
UIAlertViewStyle = 以下4种
UIAlertViewStyleDefault,
UIAlertViewStyleSecureTextInput, //密码输入框风格
UIAlertViewStylePlainTextInput, //普通输入框风格
UIAlertViewStyleLoginAndPasswordInput //账号密码框风格
*/
// An alert that allows the user to enter text. Available in iOS 5.0 and later.
alertView.alertViewStyle = UIAlertViewStylePlainTextInput; // Returns the text field at the given index
UITextField *textField = [alertView textFieldAtIndex:];
textField.text = model.title; // The number of buttons on the alert view. (read-only)
NSLog(@"The total number of button is : %ld", alertView.numberOfButtons); // Returns the title of the button at the given index.
NSLog(@"The button title at the specified index : %@", [alertView buttonTitleAtIndex:]); // The index number of the cancel button.
NSLog(@"Index for the cancel button is : %ld",alertView.cancelButtonIndex); // -1 if no otherButtonTitles or initWithTitle:... not used
NSLog(@"The index of the first other button is (read-only) : %ld",alertView.firstOtherButtonIndex); // show UIAlertView
[alertView show];
UIAlertView的一些基本属性


/**
//根据被点击按钮的索引处理点击事件
Called when a button is clicked. The view will be automatically dismissed after this call returns
*/
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; //AlertView即将显示时
-(void)willPresentAlertView:(UIAlertView *)alertView; // AlertView已经显示时的事件
-(void)didPresentAlertView:(UIAlertView *)alertView; // ALertView即将消失时的事件
// This method is invoked before the animation begins and the view is hidden.
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex; // AlertView已经消失时执行的事件
// This method is invoked after the animation ends and the view is hidden.
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex; // AlertView的取消按钮的事件
// If not defined in the delegate, we simulate a click in the cancel button
-(void)alertViewCancel:(UIAlertView *)alertView;
2、UIAlertController的使用方法:
* 使用UIAlertController共需要三步
* 1.实例化alert:alertControllerWithTitle
* 2.实例化按钮:actionWithTitle
* 3.显示alertController:presentViewController
// 1.实例化alert:alertControllerWithTitle
UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"编辑" message:@"请修改菜单名称:" preferredStyle:UIAlertControllerStyleAlert]; // 2.实例化按钮:actionWithTitle
// 为防止block与控制器间循环引用,我们这里需用__weak来预防
__weak typeof(alert) wAlert = alert;
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
// 点击确定按钮的时候, 会调用这个block
NSLog(@"%@",[wAlert.textFields.firstObject text]); }]]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; // 添加文本框(只能添加到UIAlertControllerStyleAlert的样式,如果是preferredStyle:UIAlertControllerStyleActionSheet则会崩溃)
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.text = model.title; //监听文字改变的方法
[textField addTarget:self action:@selector(textFieldsValueDidChange:) forControlEvents:UIControlEventEditingChanged];
}]; [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.secureTextEntry = YES; // 密文形式显示
textField.text = model.price;
}]; // 3.显示alertController:presentViewController
[self presentViewController:alert animated:YES completion:nil];
UIAlertController的基本使用

四、阅读提醒
在Xcode的iOS8 SDK中,UIAlertView和UIAlertController都被UIAlertController取代。官方库解释:"UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead."、"UIActionSheet is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleActionSheet instead."。说明了在iOS8+开发,UIALertView和UIActionSheet已经过时了,UIAlertController以一种模块化替换的方式来代替这两这两个控件的功能和作用。
iOS开发之UIAlertView与UIAlertController的详尽用法说明的更多相关文章
- iOS开发之Socket通信实战--Request请求数据包编码模块
实际上在iOS很多应用开发中,大部分用的网络通信都是http/https协议,除非有特殊的需求会用到Socket网络协议进行网络数 据传输,这时候在iOS客户端就需要很好的第三方CocoaAsyncS ...
- iOS开发之UISearchBar初探
iOS开发之UISearchBar初探 UISearchBar也是iOS开发常用控件之一,点进去看看里面的属性barStyle.text.placeholder等等.但是这些属性显然不足矣满足我们的开 ...
- iOS开发之UIImage等比缩放
iOS开发之UIImage等比缩放 评论功能真不错 评论开通后,果然有很多人吐槽.谢谢大家的支持和关爱,如果有做的不到的地方,还请海涵.毕竟我一个人的力量是有限的,我会尽自己最大的努力大家准备一些干货 ...
- iOS开发之 Xcode6 添加xib文件,去掉storyboard的hello world应用
iOS开发之 Xcode6.1创建仅xib文件,无storyboard的hello world应用 由于Xcode6之后,默认创建storyboard而非xib文件,而作为初学,了解xib的加载原理 ...
- iOS开发之loadView、viewDidLoad及viewDidUnload的关系
iOS开发之loadView.viewDidLoad及viewDidUnload的关系 iOS开发之loadView.viewDidLoad及viewDidUnload的关系 标题中所说的3个方 ...
- iOS开发之info.pist文件和.pch文件
iOS开发之info.pist文件和.pch文件 如果你是iOS开发初学者,不用过多的关注项目中各个文件的作用.因为iOS开发的学习路线起点不在这里,这些文件只会给你学习带来困扰. 打开一个项目,我们 ...
- iOS开发之WKWebView简单使用
iOS开发之WKWebView简单使用 iOS开发之 WKWebVeiw使用 想用UIWebVeiw做的,但是突然想起来在iOS8中出了一个新的WKWebView,算是UIWebVeiw的升级版. ...
- iOS 开发之Block
iOS 开发之Block 一:什么是Block.Block的作用 UI开发和网络常见功能的实现回调,按钮事件的处理方法是回调方法. 1. 按钮事件 target action 机制. 它是将一 ...
- iOS开发之Xcode常用调试技巧总结
转载自:iOS开发之Xcode常用调试技巧总结 最近在面试,面试过程中问到了一些Xcode常用的调试技巧问题.平常开发过程中用的还挺顺手的,但你要突然让我说,确实一脸懵逼.Debug的技巧很多,比如最 ...
随机推荐
- 【分享】学长的安利来了~~O(∩_∩)O
前言:应栋哥要求,学长把演讲稿稍微整理下发布出来,这可以算是一篇安利文,也可以说是一篇经历文吧.作为一个确确实实从软工里收获到挺多东西的过来人,学长希望可以通过学长的经历来让你们对软工更加期待. 安利 ...
- SpringIOC使用扩展
在上篇博客中,我们使用Spring通过setter访问器实现了对属性的赋值,这种做法被称为设值注入.除此之外Spring还提供了通过构造方法赋值的能力,成为构造注入.下面我们通过一个小demo来了解如 ...
- 浅谈Jquery中的bind(),live(),delegate(),on()绑定事件方式
前言 因为项目中经常会有利用jquery操作dom元素的增删操作,所以会涉及到dom元素的绑定事件方式,简单的归纳一下bind,live,delegate,on的区别,以便以后查阅,也希望该文章日后能 ...
- 原生JS 获取浏览器、窗口、元素等尺寸的方法及注意事项
一.通过浏览器获得屏幕的尺寸 screen.width screen.height screen.availHeight //获取去除状态栏后的屏幕高度 screen.availWidth //获取去 ...
- 【原创】Windows平台搭建Kafka源代码开发环境(Eclipse版本)
最近在研究Kafka源代码,需要自己搭建一个开发环境.官网上给出的提示略显简单,照着做了一遍也碰到了一些问题.特此记录下来. 开发环境: Oracle Java 1.7_u71 + Eclipse 4 ...
- C语言学习007:重定向标准输入和输出
先来完成一个将输入数据转换成json格式输出的小任务 #include <stdio.h> int main(){ float latitude; float longtitude; ]; ...
- Razor练习3
Razor语法中,物质循环处理,它使用到三种: for, while,foreach. 下面Insus.NET分别在ASP.NET MVC环境中列举一个例子,附加源代码: while:<br / ...
- Mysql高并发优化
一.数据库结构的设计 1.数据行的长度不要超过8020字节,如果超过这个长度的话在物理页中这条数据会占用两行从而造成存储碎片,降低查询效率. 2.能够用数字类型的字段尽量选择数字类型而不用字符串类型的 ...
- Ado.net 三[SQL注入,DataAdapter,sqlParameter,DataSet]
1.SQL注入:SQL注入攻击是web应用程序的一种安全漏洞,可以将不安全的数据提交给运用程序,使应用程序在服务器上执行不安全的sql命令.使用该攻击可以轻松的登录运用程序. 例如:该管理员账号密码为 ...
- What Can Java Technology Do?
What Can Java Technology Do? The general-purpose(多用途的), high-level Java programming language is a po ...