UIAlertView 與 UIActionSheet UIAlertView 樣式 實現 註意 其“確定”按鈕的顏色與“取消”按鈕的外觀一樣(沒有顯示紅色,即 normal) UIActionSheet 樣式 實現 註意 其“確定”按鈕的顏色與“取消”按鈕的外觀不一樣(顯示紅色,即 destruc ...
UIAlertView 與 UIActionSheet
- UIAlertView
樣式
實現
- (void)showAlertView { self.alertView = [[UIAlertView alloc] initWithTitle:@"確定操作嗎?" message:@"確定可能會有災難哦!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil]; self.alertView.delegate = self; [self.alertView show]; }
- 註意
- 其“確定”按鈕的顏色與“取消”按鈕的外觀一樣(沒有顯示紅色,即 normal)
- UIActionSheet
樣式
實現
- (void)showActionSheet { self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"確定操作嗎?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"確定" otherButtonTitles:nil, nil]; [self.actionSheet showInView:self.view]; }
- 註意
- 其“確定”按鈕的顏色與“取消”按鈕的外觀不一樣(顯示紅色,即 destructive)
UIAlertController
- 概述
UIAlertController出現的原因,我想就不必多說了。來看看蘋果官方的介紹吧!
A UIAlertController object displays an alert message to the user. This class replaces the UIActionSheet and UIAlertView classes for displaying alerts. After configuring the alert controller with the actions and style you want, present it using the presentViewController:animated:completion: method UIAlertController 實例是用來向用戶警告信息的。該類旨在替代 UIActionSheet 和 UIAlertView。若你按照自己的需求配置了 UIAlertController 的 actions 和 style,就使用 presentViewController:animated:completion: 方法來顯示它吧
- UIAlertController 的使用步驟
- 初始化 UIAlertController, 並設置標題,副標題,alert的樣式(alert 或 actionSheet)
- 添加事件
- 使用 UIAlertAction 定義每一個事件,與事件相關的 title、style、action
- style
- UIAlertActionStyleDefault
- UIAlertActionStyleCancel
- UIAlertActionStyleDestructive
- action
- 使用 block 代替了原來的 代理模式
- 顯示 alert
- UIAlertController 的簡單使用
- 顯示 alertView
初始化 UIAlertController
self.alertController = [UIAlertController alertControllerWithTitle:@"確定操作嗎?" message:@"確定可能會有災難哦!" preferredStyle:UIAlertControllerStyleAlert];
添加事件(
事件的添加順序,會影響按鈕的顯示順序
)UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //TODO: }]; [self.alertController addAction:cancelAction]; UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { //TODO: }]; [self.alertController addAction:confirmAction];
顯示 alert
[self presentViewController:self.alertController animated:YES completion:^{ // TODO }];
- 顯示 actionSheet
- 在此就不再貼代碼了,把 上述代碼的 UIAlertControllerStyleAlert 改成 UIAlertControllerStyleActionSheet 試試吧
- 顯示 alertView
UIAlertController 中的 textField
- 註意
- 只能向 alert 類型的 UIAlertController 中添加 textField
向 actionSheet 類型的 UIAlertController 中添加 textField,會報運行時錯誤
Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert
- 使用 UIAlertController 實現登錄界面(
demo
)效果
- 具體實現
- 使用 CocoaPods 集成 MBProgressHUD 框架(CocoaPods的安裝和使用那些事(Xcode 7.2,iOS 9.2,Swift))
編輯 podfile 文件,如下:
為了使用的方便,通常會為 MBProgressHUD 添加分類,在此只添加 showMessage 方法,如下
+ (void)showMessage:(NSString *)message { // hud 顯示的 view UIView *contentView = [[UIApplication sharedApplication].windows lastObject]; // hud MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:contentView animated:YES]; // hud 顯示的信息 hud.detailsLabelText = message; // 當 hud 隱藏時是否從父控制項中移除 hud.removeFromSuperViewOnHide = YES; // hub 顯示的時間 [hud hide:YES afterDelay:1.5f]; }
- 設置 UIAlertController
初始化 UIAlertController
self.alertController = [UIAlertController alertControllerWithTitle:@"登錄" message:nil preferredStyle:UIAlertControllerStyleAlert];
添加 textField(
textField 與 action 的添加順序,不影響其顯示順序
)[self.alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"賬戶"; }]; [self.alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"密碼"; }];
添加事件
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // TODO: }]; [self.alertController addAction:cancelAction]; UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { // get the account and password UITextField *accountTextField = self.alertController.textFields[0]; UITextField *passwordTextField = self.alertController.textFields[1]; NSString *message = [NSString stringWithFormat:@"賬戶:%@\n密碼:%@", accountTextField.text, passwordTextField.text]; // 顯示 MBProgressHUD(需要在主線程中顯示) dispatch_async(dispatch_get_main_queue(), ^{ [MBProgressHUD showMessage:message]; }); }]; [self.alertController addAction:confirmAction];
顯示 alert
[self presentViewController:self.alertController animated:YES completion:^{ // TODO }];
- 使用 CocoaPods 集成 MBProgressHUD 框架(CocoaPods的安裝和使用那些事(Xcode 7.2,iOS 9.2,Swift))