iOS開發之UIAlerView與UIAlertController的使用


一、UIAlerView

UIAlertView已經過期了,9.0或以上的版本使用會提示過期提醒,但是可以正常使用。最好還是改為使用UIAlertController替代。


warning

1. 定制彈框的標題、內容和按鈕

...
@property (nonatomic, strong) UIAlertView *alert; ... self.alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"此UIAlertView已經過期,應該使用UIAlertController替代" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的",@"知道了",nil];

2. 定制彈框的樣式

有四種樣式:
UIAlertViewStyleDefault : 沒有輸出框,只是普通的彈框
UIAlertViewStyleSecureTextInput 有一個安全密碼輸入框
UIAlertViewStylePlainTextInput 有一個普通文本輸入框
UIAlertViewStyleLoginAndPasswordInput 有兩個輸入框:文本和密碼

[self.alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; [self.alert show]; // 執行顯示彈框

showAlert

3. 彈框中的普通文本和密碼輸入框使用

// 當前控制器遵守代理:<UIAlertViewDelegate> ... @property (nonatomic, strong) UITextField *textField; @property (nonatomic, strong) UITextField *passwordField; ... self.textField = [self.alert textFieldAtIndex:0]; self.passwordField = [self.alert textFieldAtIndex:1]; self.textField.placeholder = @"用戶民"; self.passwordField.placeholder = @"密碼"; #pragma mark - UIAlerViewDelegate /** * 根據用戶按下不同按鈕執行不同的邏輯 * * @param alertView 彈框 * @param buttonIndex 按鈕索引 */ - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 1) { NSLog(@"按下了 好的 按鈕"); NSLog(@"用戶名:%@,密碼:%@",self.textField.text,self.passwordField.text); }else if (buttonIndex == 2) { NSLog(@"按下了 知道了 按鈕"); }else if (buttonIndex == 3) { NSLog(@"按下了 贊一個 按鈕"); } } /** * 決定第一個按鈕是否啟用 * * @param alertView 彈框 * * @return 布爾值 */ - (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView { if ([[self.textField text]length] == 0 || [[self.passwordField text]length] == 0) { return NO; } return YES; }

4. 其他屬性

應用退到后台彈框還是沒有消失,這個方法可以給你控制按鈕的選擇

[self.alert dismissWithClickedButtonIndex:2 animated:YES];
[self.alert addButtonWithTitle:@"贊一個"]; NSLog(@"按鈕的個數為:%ld",[self.alert numberOfButtons]); NSLog(@"取消按鈕的索引為:%ld",[self.alert cancelButtonIndex]); NSLog(@"彈框是否可見:%d",[self.alert isVisible]); NSLog(@"第2個索引的按鈕標題為:%@",[self.alert buttonTitleAtIndex:2]); NSLog(@"第一個其他按鈕的索引:%ld",[self.alert firstOtherButtonIndex]);

二、UIAlertController

在iOS 8.0 版本中新增了 UIAlertController 來取代 UIAlertView,它集合了alertactionSheet

1. 定制彈框的標題、內容和按鈕

- (void)showAlert { __weak typeof(self) weakself = self; self.alert = [UIAlertController alertControllerWithTitle:@"UIAlertController" message:@"8.0以上使用UIAlertController替換UIAlertView" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"點擊了取消按鈕"); [[NSNotificationCenter defaultCenter]removeObserver:weakself name:UITextFieldTextDidChangeNotification object:nil]; }]; UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"點擊了好的按鈕"); NSLog(@"用戶名:%@",[[[self.alert textFields]firstObject]text]); NSLog(@"密碼:%@",[[[self.alert textFields]lastObject]text]); [[NSNotificationCenter defaultCenter]removeObserver:weakself name:UITextFieldTextDidChangeNotification object:nil]; }]; // 先凍結 “好的” 按鈕,需要用戶輸入用戶名和密碼后再啟用 [defaultAction setEnabled:NO]; [self.alert addAction:cancleAction]; [self.alert addAction:defaultAction]; // 添加文本輸入框 [self.alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"請輸入用戶名"; [[NSNotificationCenter defaultCenter]addObserver:weakself selector:@selector(handleTextFieldDidChanged:) name:UITextFieldTextDidChangeNotification object:nil]; }]; // 添加密碼輸入框 [self.alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"請輸入密碼"; textField setSecureTextEntry:YES]; }]; [self presentViewController:self.alert animated:YES completion:nil]; } - (void)handleTextFieldDidChanged:(NSNotification *)notification { UIAlertController *alertController = (UIAlertController *)self.presentedViewController; if (alertController) { UITextField *textField = alertController.textFields.firstObject; UIAlertAction *action = alertController.actions.lastObject; action.enabled = textField.text.length > 0; } }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM