本文共 3900 字,大约阅读时间需要 13 分钟。
1.默认样式
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"你好" message:@"这个是UIAlertController的默认样式" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"点击了取消"); }]; UIAlertAction *commitAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"点击了确认"); }]; [alertController addAction:cancleAction]; [alertController addAction:commitAction]; [self presentViewController:alertController animated:YES completion:nil];
2.输入框样式
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"文本对话框" message:@"登陆和密码对话框" preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"登陆"; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField]; }]; [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"密码"; /** * 是否隐藏输入 */ textField.secureTextEntry = YES; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField]; }]; UIAlertAction *commitAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { /** * 这里面可以做一些与服务器端交互的事情 */ NSLog(@"%@\n", alertController.textFields.firstObject.text); NSLog(@"%@", alertController.textFields.lastObject.text); [[NSNotificationCenter defaultCenter] removeObserver:self]; }]; commitAction.enabled = NO; UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"用户取消了登陆"); [[NSNotificationCenter defaultCenter] removeObserver:self]; }]; /** * 添加交互按钮 */ [alertController addAction:cancleAction]; [alertController addAction:commitAction]; [self presentViewController:alertController animated:YES completion:nil];
/** * 监听textField是否改变 */- (void)alertTextFieldDidChange:(NSNotification *)nc{ UIAlertController *alertController = (UIAlertController *)self.presentedViewController; if (alertController) { UITextField *userField = alertController.textFields.firstObject; UITextField *passWordField = alertController.textFields.lastObject; UIAlertAction *commitAction = alertController.actions.lastObject; commitAction.enabled = userField.text.length > 2 && passWordField.text.length > 6; NSLog(@"%d", commitAction.enabled); }}3.上拉菜单样式
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"保存和删除数据" message:@"删除数据不可恢复" preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"取消"); }]; UIAlertAction *commitAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"确定"); }]; UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"保存"); }]; [alertController addAction:cancleAction]; [alertController addAction:commitAction]; [alertController addAction:saveAction]; [self presentViewController:alertController animated:YES completion:nil];