1:自带单个输入框的UIAlertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"请输入备注信息" preferredStyle:UIAlertControllerStyleAlert];
//增加取消按钮;
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
//增加确定按钮;
[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//获取第1个输入框;
UITextField *userNameTextField = alertController.textFields.firstObject;
self.remarkLabel.text =userNameTextField.text;
NSLog(@"备注信息 = %@",userNameTextField.text);
}]];
//定义第一个输入框;
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"备注信息";
textField.secureTextEntry = NO;
}];
[self presentViewController:alertController animated:true completion:nil];
2:两个输入框的UIAlertController
// 1.创建UIAlertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Login"
message:@"Enter Your Account Info Below"
preferredStyle:UIAlertControllerStyleAlert];
// 2.1 添加文本框
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"username";
[textField addTarget:self action:@selector(alertUserAccountInfDidChange:) forControlEvents:UIControlEventEditingChanged]; // 添加响应事件
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"password";
textField.secureTextEntry = YES;
[textField addTarget:self action:@selector(alertUserAccountInfDidChange:) forControlEvents:UIControlEventEditingChanged]; // 添加响应事件
}];
// 2.2 创建Cancel Login按钮
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"Cancel Action");
}];
UIAlertAction *loginAction = [UIAlertAction actionWithTitle:@"Login" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *userName = alertController.textFields.firstObject;
UITextField *password = alertController.textFields.lastObject;
// 输出用户名 密码到控制台
NSLog(@"username is %@, password is %@",userName.text,password.text);
}];
// 2.3 添加按钮
loginAction.enabled = NO; // 禁用Login按钮
[alertController addAction:cancelAction];
[alertController addAction:loginAction];
// 3.显示警报控制器
[self presentViewController:alertController animated:YES completion:nil];
// 输入内容长度的判断
- (void)alertUserAccountInfDidChange:(UITextField *)sender
{
UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
if (alertController)
{
UITextField *userName = alertController.textFields.firstObject;
UIAlertAction *loginAction = alertController.actions.lastObject;
UITextField *password = alertController.textFields.lastObject;
if (userName.text.length>3 && password.text.length > 6) // 用户名必须大于三位 密码必须大于六位
{
loginAction.enabled = YES;
}
}
}
3:三个按钮事件的UIAlertController
// 1.创建UIAlertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert Title"
message:@"The message is ..."
preferredStyle:UIAlertControllerStyleAlert];
// 2.创建并添加按钮
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"OK Action");
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"Cancel Action");
}];
UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"Reset" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"Reset Action");
}];
[alertController addAction:cancelAction]; // B
[alertController addAction:okAction]; // A
[alertController addAction:resetAction]; // C
// 3.呈现UIAlertContorller
[self presentViewController:alertController animated:YES completion:nil];
4:底部弹出的UIAlertContorller
// 1.创建UIAlertController
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
// 2.1 创建按钮
UIAlertAction *Action1 = [UIAlertAction actionWithTitle:@"单人悬赏" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *Action2 = [UIAlertAction actionWithTitle:@"多人悬赏" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *Action3 = [UIAlertAction actionWithTitle:@"招标任务" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
// 2.2 添加按钮
[actionSheetController addAction:Action1];
[actionSheetController addAction:Action2];
[actionSheetController addAction:Action3];
[actionSheetController addAction:cancelAction];
// 3.显示警报控制器
[self presentViewController:actionSheetController animated:YES completion:nil];
5:普通款式的UIAlertController
UIAlertController *alterController = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定删除该好友吗?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *actionSubmit = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
[alterController addAction:actionCancel];
[alterController addAction:actionSubmit];
[self presentViewController:alterController animated:YES completion:^{}];