本文将从四个方面对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:0]; 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:1]); // 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];