iOS UIAlertController,uialertcontroller
在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];