Alert Views
Alert views display a concise and informative alert message to the user. Alert views convey important information about an app or the device, interrupting the user and requiring them to stop what they’re doing to choose an action or dismiss the alert. For example, iOS uses alerts to warn the user that battery power is running low, so they can connect a power adapter before their work is interrupted. An alert view appears on top of app content, and must be manually dismissed by the user before he can resume interaction with the app.

Purpose. Alert views allow users to:
- Be immediately informed of critical information
- Make a decision about a course of action
Implementation. Alert views are implemented in the UIAlertView class and discussed in the UIAlertView Class Reference.
Configuration. Alert views are created, initialized, and configured in code, typically residing in a view controller implementation file.
Content of Alert Views (Programmatic)
You cannot create or manipulate alert views in Interface Builder. Rather, an alert view floats over an existing view to interrupt its presentation, and it requires the user to dismiss it. If an alert view contains a custom button enabling the users to choose an alternative action, rather than simply dismissing the alert, that action is handled by the alert view’s delegate.
When setting alert view content, you can control the number of buttons, their titles, displayed text, and inclusion of one or two text fields, one of which can be a secure text-input field.
When you create an alert view object from the UIAlertView class, you can initialize its most important properties with one method, initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:. Depending on your app’s needs, that single message may be enough to configure a fully functional alert object, as shown in the following code. After you have created the alert object, send it a show message to display the alert.
- UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:@"Title"
- message:@"This is the message."
- delegate:self
- cancelButtonTitle:@"OK"
- otherButtonTitles:nil];
- [theAlert show];
Every alert has a Cancel button so that the user can dismiss the alert. You can add additional, custom buttons to enable the user to perform some other action related to the alert, such as rectifying the problem the alert warned about. Although you can add multiple custom buttons to an alert, iOS Human Interface Guidelines recommend that you limit alerts to two buttons, and consider using an action sheet instead if you need more.
To add a titled custom button to an alert, send it an addButtonWithTitle: message. Alternatively, you can pass the custom button title, followed by a comma and nil terminator, with the otherButtonTitles: parameter when you initialize the alert view object.
Optionally, an alert can contain one or two text fields, one of which can be a secure text-input field. You add text fields to an alert after it is created by setting its alertViewStyle property to one of the styles specified by the UIAlertViewStyle constants. The alert view styles can specify no text field (the default style), one plain text field, one secure text field (which displays a bullet character as each character is typed), or two text fields (one plain and one secure) to accommodate a login identifier and password.

Behavior of Alert Views (Programmatic)
If your alert has a custom button, you must designate a delegate to handle the button’s action, and the delegate must conform to the UIAlertViewDelegate protocol. You designate the delegate with the delegate parameter when you initialize the alert view object. The delegate must implement the alertView:clickedButtonAtIndex: message to respond when the custom button is tapped; otherwise, your custom buttons do nothing. For example, the following code shows an implementation that simply logs the title of the button that was tapped:
- - (void)alertView:(UIAlertView *)theAlert clickedButtonAtIndex:(NSInteger)buttonIndex
- {
- NSLog(@"The %@ button was tapped.", [theAlert buttonTitleAtIndex:buttonIndex]);
- }
In the delegate method alertView:clickedButtonAtIndex:, depending on which button the user tapped, you can retrieve the values of text fields in your alert view with the textFieldAtIndex: method.
- if (theAlert.alertViewStyle == UIAlertViewStyleLoginAndPasswordInput) {
- NSLog(@"The login field says %@, and the password is %@.",
- [theAlert textFieldAtIndex:0].text, [theAlert textFieldAtIndex:1].text);
- }
The alert view is automatically dismissed after the alertView:clickedButtonAtIndex: delegate method is invoked. Optionally, you can implement the alertViewCancel: method to take the appropriate action when the system cancels your alert view. An alert view can be canceled at any time by the system—for example, when the user taps the Home button. If the delegate does not implement the alertViewCancel: method, the default behavior is to simulate the user clicking the cancel button and closing the view.
Appearance of Alert Views
You cannot customize the appearance of alert views.
Using Auto Layout with Alert Views
The layout of alert views is handled for you. You cannot create Auto Layout constraints between an alert view and another user interface element.
For general information about using Auto Layout with iOS views, see Using Auto Layout with Views.
Making Alert Views Accessible
Alert views are accessible by default.
Accessibility for alert views pertains to the alert title, alert message, and button titles. If VoiceOver is activated, it speaks the word “alert” when an alert is shown, then speaks its title followed by its message if set. As the user taps a button, VoiceOver speaks its title and the word “button.” As the user taps a text field, VoiceOver speaks its value and “text field” or “secure text field.”
For general information about making iOS views accessible, see Making Views Accessible.
Internationalizing Alert Views
To internationalize an alert view, you must provide localized translations of the alert title, message, and button titles. Screen metrics and layout may change depending on the language and locale.
For more information, see Internationalization and Localization Guide.
Debugging Alert Views
When debugging issues with alert views, watch for this common pitfall:
Not testing localizations. Be sure to test the alert views in your app with the localizations you intend to ship. In particular, button titles can truncate if they are longer in localizations other than the one in which you designed your user interface. Following the HI guideline to give buttons short, logical titles helps to ameliorate this potential problem, but localization testing is also required.
Elements Similar to an Alert View
The following elements provide similar functionality to an alert view:
- Action Sheet. Present an action sheet when users tap a button in a toolbar, giving them choices related to the action they’ve initiated. For more information, see Action Sheets.
- Modal View. Present a modal view (that is, the view controller uses a modal presentation style) when users initiate a subtask in the context of their workflow or another task. For more information, see UIViewController Class Reference.
Alert Views的更多相关文章
- iOS--UIAlertView与UIAlertController和UIAlertAction之间的事儿
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备 ...
- iOS 8.0后使用UIAlertController
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...
- UIAlertController 使用
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...
- 在iOS 8中使用UIAlertController
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...
- iOS之AlertController的使用
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController 在实现视图控制器间的过渡动画效果和自适应设备尺 ...
- iOS8 iPad Warning: Attempt to present <UIImagePickerController:xxxx > on xxxx which is already presenting (null)
解决方法: /* I think this is because in iOS 8, alert views and action sheets are actually presented view ...
- UIAlertController 的使用(NS_CLASS_AVAILABLE_IOS(8_0)iOS8以后有效)
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController 在实现视图控制器间的过渡动画效果和自适应设备尺 ...
- IOS后台执行机制 与 动作
当用户按下"Home"键或者系统启动另外一个应用时,前台foreground应用首先切换到Inactive状态,然后切换到Background状态.此转换将会导致先后调用应用代理的 ...
- iOS 最新UIAlertController
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController 在实现视图控制器间的过渡动画效果和自适应设备尺 ...
随机推荐
- Python 代码实现模糊查询
Python 代码实现模糊查询 1.导语: 模糊匹配可以算是现代编辑器(如 Eclipse 等各种 IDE)的一个必备特性了,它所做的就是根据用户输入的部分内容,猜测用户想要的文件名,并提供一个推荐列 ...
- 转载:C#中&与&&的区别
原文地址:http://www.cnblogs.com/chinafine/archive/2009/02/17/1392309.html 感谢博主分享! 二元运算符 (&) 为整型和 b ...
- VB编程技巧推荐
VB编程技巧推荐 1.zyl910的专栏——理论水平高 用VB写高效的图像处理程序 V2.0 优化分支代码——避免跳转指令堵塞流水线 2.Laviewpbt的专栏 —— 有很多算法的代码,实用性高 ...
- ZOJ1463
题意:给一个括号字符串,求解最少添加的字符能使整个字符串匹配. 输入: s(未匹配的字符串) 输出: S(匹配后的字符串) 思路:绝壁超级坑的一道题,格式我不想说什么了,特坑,然后就是对给定的字符串, ...
- html.ex.day01
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Swift - 29 - 参数的默认值
// 参数设置了默认值之后, 在调用的时候, 可以写这个参数 // 在参数前面添加"_", 表示取消外部参数名, 但还是建议使用苹果默认格式 func sayHello(nickN ...
- ASP.NET菜鸟之路之Request小例子
背景 我是一个ASP.NET菜鸟,暂时开始学习ASP.NET,在此记录下我个人敲的代码,没有多少参考价值,请看到的盆友们为我点个赞支持我一下,多谢了. Request获取值 Request获取值有两种 ...
- ASP.NET中实现页面间数据传递的方法
说到页面间数据传递,很多人都会想到通过像Session这样的全局变量,但是向Session中添加的东西太多会增加服务器的压力,页面间数据传递,数据的作用范围越小越好. ASP.NET页面间数据传递 ...
- AngularJs的Select演示
昨天需要在项目使用Angular.js的select,测试了好久才研究出怎么进行赋值,操作. HTML代码 <!DOCTYPE html> <html> <head> ...
- jquery如何将获取的颜色值转换为十六进制形式
jquery如何将获取的颜色值转换为十六进制形式:大家或许已经注意到了,在谷歌.火狐和IE8以上浏览器中,获取的颜色值是RGB形式,例如rgb(255,255,0),感觉非常不适应,或者在实际编码中不 ...