UIKit User Interface Catalog Part One (Alert Views)


链接:http://pan.baidu.com/s/1i5GKG9v 密码:9g6z


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.

Alert views将准确的、有用信息的警告消息展现给用户。Alert views 调查(convey)app或设备的重要信息,打断用户(的操作)要求用户停止正在做的事,选择一个action或者关闭alert。比如说,iOS用alert警告用户电量低,这样用户就可以在他们的工作中断之前连接上电源。alert view在app内容上方显示,用户必须手动关闭(alert view),才能继续与app的交互。

Alert View

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.
//alert view需要在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.

Alert Views不能在IB中创建和操作。Alert Views在已有的view上浮现,打断presentation,要求用户关闭它。如果alert view包含了自定义的button让用户来进行action的选择,而不是简单地关闭alert view,那么这个操作将会由alert view 的代理来处理。

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.

当设置alert view的内容时,你可以控制button的数量,title,displayed text,甚至包含一两个text field,其中一个还可以是安全输入框(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创建alert view对象时,你可以通过一个方法来初始化最重要的属性:initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:。根据你app 的需要,这个简单的消息可能足够用来配置完整功能的alert 对象,如下代码所示。创建alert 对象后,就可以发送 show消息来显示alert。

UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:@"Title"
                                                  message:@"This is the message."
[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.

每个alert都有Cancel button,用户可以借此来关掉alert。你可以添加额外的、自定义的button来让用户执行一些其他与alert相关的操作,比如修正alert警告的问题。尽管你可以对alert添加多个自定义的button,iOS Human Interface Guidelines建议你将alert最多显示两个按钮,如果你需要更多按钮的话请考虑使用action sheet。

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.

要给alert添加自定义的titled按钮,发送 addButtonWithTitle:消息。或者也可以在初始化alert view对象时,在otherButtonTitles: 处传一个custom button title,后跟一个逗号和nil终结符。

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.

可选地,一个alert可以包含一个或者两个text field,其中一个可以是secure text-input field。在alert创建后,通过设置 alertViewStyle 属性为UIAlertViewStyle 常量值之一,就可以给alert添加text field了。alert view style可以指明没有text field(default style),一个普通text field, 一个secure text filed, 或者两个text field(一个plain 一个secure)来接受登录identifier和密码。

两个text field的示例

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:

如果你的alert有一个自定义按钮,那么你必须赋值一个代理来处理button的action,代理必须服从UIAlertViewDelegate协议。初始化alert view对象时用delegate参数来设置代理。代理必须实现alertView:clickedButtonAtIndex: 消息来对自定义按钮的点击进行响应。否则的话,你的自定义按钮就什么都做不了。比如说,下面的代码展示了输出button title的代码实现。

- (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:

在代理方法alertView:clickedButtonAtIndex:中,根据用户点击的按钮,你可以用 textFieldAtIndex:方法获取alert view中text field的中的值。

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.

alert view在 alertView:clickedButtonAtIndex: 协议方法触发后自动消失。可选地,你可以实现alertViewCancel: 方法来在系统取消alert view时采取合适的操作。alert view可以在任何时候被系统所取消--比如说,当用户点击了Home Button。如果代理不实现alertViewCancel: 方法,默认的行为将会是当做用户点击了取消按钮,关闭view。

Appearance of Alert Views

You cannot customize the appearance of alert views.
//alert view的外观是不支持自定义的。

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.

alert view的布局不需要你处理。在alert view和其他界面元素之间你不能创建Auto Layout约束。

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.

debug alert view时,注意以下陷阱:
Not testing localizations.

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 view提供的功能相似:

Action Sheet.在用户点击toolbar中的按钮时展示一个action sheet,给出用户触发的与行为相关的选项。
Modal View.当用户在workflow的上下文中或者其他任务中创建了一个子任务时,展示一个 modal view (that is, the view controller uses a modal presentation style) 。For more information, see UIViewController Class Reference.