iOS8新特性-UIAlertController
UIAlertController
iOS8釋出也有半年之久了, 下面由小編給大家簡單描述概述下關於iOS8的新特性:
- UIAlertController
- UIPopoverPresentationController
- UIPresentationController
- Sizeclass
UIAlertController
- 回顧UIAlertView
- 回顧UIActionSheet
- UIAlertController詳解
回顧UIAlertView
UIAlertView 在iOS 8之前的iOS版本上執行的應用程式,使用UIAlertView類顯示警告資訊給使用者。與其功能類似,但顯示樣式不同, 同為警告檢視的是UIActionSheet類(詳見回顧UIActionSheet) —— [iOS8.1 API]
相信UIAlertView對大家來說一定不陌生,因此小編在這裡只帶大家回顧一下UIAlertView的簡單用法。
// 建立並初始化alert
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你該吃飯了" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
// 給alert新增文字輸入框
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
// 給alert新增密文輸入框
// alertView.alertViewStyle = UIAlertViewStyleSecureTextInput;
// 給alert新增登陸輸入框
// alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
// 顯示alert
[alertView show];
其實, UIAlertView的用法很簡單,也很實用。呼叫show方法就可以得到我們想要的。
回顧UIActionSheet
// 建立一個UIActionSheet物件
UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"提示" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"確定" otherButtonTitles:@"你猜", nil];
// 將action顯示在要顯示的view上
[action showInView:self.view];
UIActionSheet在iOS8中被棄用了,因此只是簡單給大家介紹一下。我們的重點還是UIAlertController。
UIAlertController詳解
在iOS8中更新的UIAlertController將兩種警告檢視融合成了一個檢視控制器,並且能夠更好的定製,方便了開發人員。
UIAlertController = UIAlertView + UIActionSheet
檢視UIAlertController的標頭檔案,可得知其繼承自UIViewController,因此它是一個檢視控制器。如下圖:
我們可得知該類有個類方法,用於讓我們對其設定內容。
+ (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;
該類方法方便我們設定提示的標題和提示內容,和我們所需要顯示的樣式。UIAlertControllerStyle是個列舉型別。
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,
UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);
由該列舉型別我們可以看出,UIAlertController有兩種樣式,分別是UIAlertControllerStyleActionSheet和UIAlertControllerStyleAlert
UIAlertControllerStyleAlert
當我們選擇UIAlertControllerStyleAlert時,並其加入到- (void)touchesBegan:(NSSet )touches withEvent:(UIEvent )event方法中時
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"你該睡覺了" preferredStyle:UIAlertControllerStyleAlert];
執行程式,發現並沒有任何反應,而且檢視標頭檔案發現UIAlertController並沒有show方法,因為UIAlertController是一個檢視控制器,則嘗試使用模態推出的方式顯示這個UIAlertController
[self presentViewController:alert animated:YES completion:^{
}];
再次執行,發現成功了
但是並沒有像之前UIAlertView的一模一樣,與之前不同的是,缺少了按鈕。因為我們並沒有新增按鈕,再去檢視標頭檔案發現
- (void)addAction:(UIAlertAction *)action;
又多了一個新類UIAlertAction,我們試著建立一個並新增進去
// 建立一個UIAlertAction 選擇其樣式為UIAlertActionStyleDestructive(毀滅性的)
UIAlertAction *action = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
}];
// 向UIAlertAction中新增action
[alert addAction:action];
執行之後,發現我們想要的按鈕出來了,並且可以像ActionSheet那樣顯示紅色的按鈕文字。
UIAlertControllerStyleActionSheet
初始化方法和顯示方式都同上,只不過改一下樣式,在此則不做程式碼展示了。
給大家看下執行後的結果
iOS8之後,將AlertView和ActionSheet規整成了一個檢視控制器,方便了開發人員的自己定製自己想要的警告檢視。
相關文章
- [轉]iOS8之後棄用UIAlertView,改用UIAlertController的詳細說明iOSUIViewController
- UIAlertController字型顏色UIController
- 新特性
- Oracle11新特性——PLSQL新特性(七)OracleSQL
- Oracle11新特性——PLSQL新特性(六)OracleSQL
- Oracle11新特性——PLSQL新特性(五)OracleSQL
- Oracle11新特性——PLSQL新特性(四)OracleSQL
- Oracle11新特性——PLSQL新特性(三)OracleSQL
- Oracle11新特性——PLSQL新特性(二)OracleSQL
- Oracle11新特性——PLSQL新特性(一)OracleSQL
- 【ORACLE新特性】11G 分割槽新特性Oracle
- React 新特性React
- 新特性介面
- 新特性方面
- UITableView 點選事件建立UIAlertControllerUIView事件Controller
- 10G新特性筆記之安裝新特性筆記
- Servlet 3.0 新特性Servlet
- Go 1.13 新特性Go
- Java 11新特性Java
- PHP 7.4 新特性PHP
- Java 17新特性Java
- Prometheus 2.21.0 新特性Prometheus
- Java 8 新特性Java
- Hadoop新特性Hadoop
- MySQL 8.0 新特性MySql
- C++新特性C++
- PostgreSQL 13–新特性SQL
- Java 8 新特性Java
- .NET 7新特性
- Django 2.0 新特性Django
- Java 8新特性Java
- MySQL 5.7新特性MySql
- Git 1.8.5 新特性Git
- JavaFX 2.2.4新特性Java
- C#新特性C#
- EJB 3.2新特性
- cassandra 1.0.0新特性
- JDK 5新特性JDK