下面來看看UIAlertController 和 UIAlertAction 用法:
1. 最簡單的提醒視圖:
這裏我們實現壹個最簡單的提醒視圖,包含1個標題,1行信息,1個按鍵,按下按鍵後,什麽都不發生:
[objc] view plain copy
- (IBAction)doAlert:(id)sender {
// 準備初始化配置參數
NSString *title = @"Alert Button Selected";
NSString *message = @"I need your attention NOW!";
NSString *okButtonTitle = @"OK";
// 初始化
UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
// 創建操作
UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// 操作具體內容
// Nothing to do.
}];
// 添加操作
[alertDialog addAction:okAction];
// 呈現警告視圖
[self presentViewController:alertDialog animated:YES completion:nil];
}
進入程序後,點擊“Alert Me!”按鈕可觸發這個提醒框,如圖所示:
從代碼可以看出,新的API更加符合邏輯,有種需要什麽就加什麽的感覺。
2. 多個按鍵的提醒視圖
這裏我們實現壹個最簡單的提醒視圖,包含1個標題,1行信息,3個按鍵,按下按鍵後,標簽顯示按下的按鍵名稱:
[objc] view plain copy
- (IBAction)doMultiButtonAlert:(id)sender {
// 準備初始化配置參數
NSString *title = @"Alert Button Selected";
NSString *message = @"I need your attention NOW!";
NSString *okButtonTitle = @"OK";
NSString *neverButtonTitle = @"Never";
NSString *laterButtonTitle = @"Maybe Later";
// 初始化
UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
// 分別3個創建操作
UIAlertAction *laterAction = [UIAlertAction actionWithTitle:laterButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// 普通按鍵
self.userOutput.text = @"Clicked 'Maybe Later'";
}];
UIAlertAction *neverAction = [UIAlertAction actionWithTitle:neverButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
// 紅色按鍵
self.userOutput.text = @"Clicked 'Never'";
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// 取消按鍵
self.userOutput.text = @"Clicked 'OK'";
}];
// 添加操作(順序就是呈現的上下順序)
[alertDialog addAction:laterAction];
[alertDialog addAction:neverAction];
[alertDialog addAction:okAction];
// 呈現警告視圖
[self presentViewController:alertDialog animated:YES completion:nil];
}
3個按鍵分別代表了3種不同類型的按鍵,分別是默認按鍵(普通)、銷毀按鍵(紅色)和取消按鍵(粗體)。從代碼看其實就是在上壹個的基礎上加了3個 UIAlertAction 而已,然後分別設置不同的 style,效果如下:
3. 帶輸入框的提醒視圖
如何添加輸入框呢?新的 iOS 8 提供了相應的接口,使增加輸入框就像增加按鍵方法壹樣簡單。這裏還是在第1個方法的基礎上改動。
[objc] view plain copy
- (IBAction)doAlertInput:(id)sender {
// 準備初始化配置參數
NSString *title = @"Email Address";
NSString *message = @"Please enter your your email address:";
NSString *okButtonTitle = @"OK";
// 初始化
UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
// 創建文本框
[alertDialog addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"Your Email";
textField.secureTextEntry = NO;
}];
// 創建操作
UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// 讀取文本框的值顯示出來
UITextField *userEmail = alertDialog.textFields.firstObject;
self.userOutput.text = userEmail.text;
}];
// 添加操作(順序就是呈現的上下順序)
[alertDialog addAction:okAction];
// 呈現警告視圖
[self presentViewController:alertDialog animated:YES completion:nil];
}
在創建操作前先創建文本框,以便後面的按鍵可以操作文本框內容。創建文本框也只是用了壹個簡單的方法而已,想創建更多文本框就再使用多次這個方法即可,程序效果如下:
4. 提醒圖表
與第2個和第3個方法相比,創建提醒圖表簡直易如反掌。因為和第1個方法相比,只需要改動壹個參數就可以,即把創建UIAlertController實例的參數 UIAlertControllerStyleAlert 改為 UIAlertControllerStyleActionSheet ,別的都不用變。
[objc] view plain copy
- (IBAction)doActionSheet:(id)sender {
// 準備初始化配置參數
NSString *title = @"Alert Button Selected";
NSString *message = @"I need your attention NOW!";
NSString *okButtonTitle = @"OK";
NSString *neverButtonTitle = @"Never";
NSString *laterButtonTitle = @"Maybe Later";
// 初始化
UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
// 分別3個創建操作
UIAlertAction *laterAction = [UIAlertAction actionWithTitle:laterButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// 普通按鍵
self.userOutput.text = @"Clicked 'Maybe Later'";
}];
UIAlertAction *neverAction = [UIAlertAction actionWithTitle:neverButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
// 紅色按鍵
self.userOutput.text = @"Clicked 'Never'";
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// 取消按鍵
self.userOutput.text = @"Clicked 'OK'";
}];
// 添加操作(順序就是呈現的上下順序)
[alertDialog addAction:laterAction];
[alertDialog addAction:neverAction];
[alertDialog addAction:okAction];
// 呈現警告視圖
[self presentViewController:alertDialog animated:YES completion:nil];
}
這個就很簡單了,跟第2個方法很像,效果如圖:
5. 播放系統聲音、提醒聲音和振動設備
在 iOS 8 中,調用聲音的方法發生了小變化,用曾經的方式無法獲取系統聲音文件的 soundID 。因此,這裏直接調用 soundID 值來調用對應的聲音,註意振動仍然正常調用kSystemSoundID_Vibrate常量即可:
[objc] view plain copy
- (IBAction)doSound:(id)sender {
// 播放系統聲音
AudioServicesPlaySystemSound(1005);
}
- (IBAction)doAlertSound:(id)sender {
// 播放提醒聲音
AudioServicesPlayAlertSound(1006);
}
- (IBAction)doVibration:(id)sender {
// 執行震動
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
這樣在 iOS 8 中就可以對用戶發出提示了。