UIKit User Interface Catalog Part One (Alert Views)

本文为大地瓜原创,欢迎知识共享,转载请注明出处。
虽然你不注明出处我也没什么精力和你计较。
作者微信号:christgreenlaw


2017-10-12日更新:
苹果官网删除了本文档,已经找不到这个文档了,好在我还有pdf版,现共享给大家。
链接:http://pan.baidu.com/s/1i5GKG9v 密码:9g6z


正如文档的名字,UIKit中的用户界面目录,其实简单的来讲,这篇文档就是把常见的界面组件列了个目录,大概介绍,而不深入。


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."
                                                   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.

每个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:
method.

在代理方法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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,029评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,395评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,570评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,535评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,650评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,850评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,006评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,747评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,207评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,536评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,683评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,342评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,964评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,772评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,004评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,401评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,566评论 2 349

推荐阅读更多精彩内容