在Xcode的iOS9.0 SDK中,UIAlertView和UIActionSheet都被UIAlertController取代。
在iOS 9中,UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一種模塊化替換的方式來代替這兩貨的功能和作用。是使用對話框(alert)還是使用上拉菜單(action sheet),就取決於在創建控制器時,您是如何設置首選樣式的。
1、對話框
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"標題"message:@"這個是UIAlertController的默認樣式"preferredStyle:UIAlertControllerStyleAlert]; [self presentViewController:alertController animated:YES completion:nil];
同創建UIAlertView相比,我們無需指定代理,也無需在初始化過程中指定按鈕。不過要特別注意第三個參數,要確定您選擇的是對話框樣式還是上拉菜單樣式。
2、將動作按鈕添加到控制器
通過創建UIAlertAction的實例,您可以將動作按鈕添加到控制器上。UIAlertAction由標題字符串、樣式以及當用戶選中該動作時運行的代碼塊組成。通過UIAlertActionStyle,您可以選擇如下三種動作樣式:常規(default)、取消(cancel)以及警示(destruective)。為了實現原來我們在創建UIAlertView時創建的按鈕效果,我們只需創建這兩個動作按鈕並將它們添加到控制器上即可。
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"標題"message:@"這個是UIAlertController的默認樣式"preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil];
3、“警示”樣式
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"標題"message:@"這個是UIAlertController的默認樣式"preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:nil]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; //添加順序和顯示順序相同 [alertController addAction:cancelAction]; [alertController addAction:resetAction]; [self presentViewController:alertController animated:YES completion:nil];
4、文本對話框
設置輸入框,並且設置通知,檢測輸入字符長度少於3時,不能點擊“好的”。
值得注意的是,不像使用UIAlertView的時候可以用UIAlertViewDelegate檢測文本框輸入,UIAlertController沒有對應的代理,只能自己實現監聽。
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"文本對話框" message:@"登錄和密碼對話框示例" preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField]; textField.placeholder = @"登錄"; }]; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField]; textField.placeholder = @"密碼"; textField.secureTextEntry = YES; }]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { UITextField *login = alertController.textFields.firstObject; UITextField *password = alertController.textFields.lastObject; NSLog(@"%@",login.text); NSLog(@"%@",password.text); }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil];
- (void)alertTextFieldDidChange:(NSNotification *)notification{ UIAlertController *alertController = (UIAlertController *)self.presentedViewController; if (alertController) { UITextField *login = alertController.textFields.firstObject; UIAlertAction *okAction = alertController.actions.lastObject; okAction.enabled = login.text.length > 2; } }
5、上拉菜單
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"保存或刪除數據" message:@"刪除數據將不可恢復" preferredStyle: UIAlertControllerStyleActionSheet]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) { NSLog(@"點擊了取消"); }]; UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"刪除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) { NSLog(@"點擊了刪除"); }]; UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { NSLog(@"點擊了保存"); }]; [alertController addAction:cancelAction]; [alertController addAction:deleteAction]; [alertController addAction:archiveAction]; [self presentViewController:alertController animated:YES completion:nil];