UIAlertView的使用

本文详细介绍了在iOS应用中如何使用UIAlertView与UIAlertController来显示警告框和操作表。包括不同样式下的配置方法、代理模式的实现以及输入模式的设置等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 方式1 无代理,只有一个确定按钮(左右排列,取消按钮在左,确定按钮在右)  
  2. UIAlertView *alertview001 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"按钮点击后我才出现的。" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"知道了", nil nil];  
  3. [alertview001 show];  


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 方式2 无代理,有多个确定按钮(多个确定按列表排列,且取消按钮在最下面)  
  2. UIAlertView *alertview002 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"按钮点击后我才出现的。" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"知道0"@"知道1"@"知道2"@"知道3"@"知道4", nil nil];  
  3. [alertview002 show];  


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 方法3 有代理,只有一个确定按钮  
  2. /* 
  3. 1 设置代理为 self 
  4. 2 设置代理协议 
  5. 3 实现代理方法 
  6. */  
  7. UIAlertView *alertview003 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"按钮点击后我才出现的。" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"知道0", nil nil];  
  8. [alertview003 show];  
  9.   
  10. // 设置协议  
  11. @interface ViewController () <UIAlertViewDelegate>  
  12.   
  13. @end  
  14.   
  15. // UIAlertViewDelegate实现代理方法  
  16. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  17. {  
  18.     NSLog(@"buttonIndex=%ld", buttonIndex);  
  19.       
  20.     // 方法1  
  21. //    if (0 == buttonIndex)  
  22. //    {  
  23. //        NSLog(@"点击了取消按钮");  
  24. //    }  
  25. //    else if (1 == buttonIndex)  
  26. //    {  
  27. //        NSLog(@"点击了确定按钮");  
  28. //    }  
  29.       
  30.     // 方法2  
  31.     NSString *title = [alertView buttonTitleAtIndex:buttonIndex];  
  32.     BOOL isEqual = [title isEqualToString:@"知道0"];  
  33.     if (isEqual)  
  34.     {  
  35.         NSLog(@"点击了确定按钮");  
  36.     }  
  37.     else  
  38.     {  
  39.         NSLog(@"点击了取消按钮");  
  40.     }  
  41. }  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 方法4 有代理,只有一个确定按钮,输入模式  
  2. UIAlertView *alertview004 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"请输入信息" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"知道0", nil nil];  
  3.       
  4. // 默认模式,无输入框  
  5. //    alertview004.alertViewStyle = UIAlertViewStyleDefault;  
  6. // 输入不可见文本的密码模式,有一个密码输入框  
  7. //    alertview004.alertViewStyle = UIAlertViewStyleSecureTextInput;  
  8. // 输入可见文本模式,有一个文本输入框  
  9. //    alertview004.alertViewStyle = UIAlertViewStylePlainTextInput;  
  10. // 输入可见文本与不可见文本模式,即登录输入模式,有两个输入框  
  11. alertview004.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;  
  12.       
  13. // 第一个输入框  
  14. UITextField *nameField = [alertview004 textFieldAtIndex:0];  
  15. nameField.placeholder = @"请输入登录帐号";  
  16. // 第二个输入框  
  17. UITextField *passwordField = [alertview004 textFieldAtIndex:1];  
  18. passwordField.placeholder = @"请输入登录密码";  
  19.   
  20. // 显示  
  21. [alertview004 show];  
  22.   
  23. // 设置协议  
  24. @interface ViewController () <UIAlertViewDelegate>  
  25.   
  26. @end  
  27.   
  28. // UIAlertViewDelegate实现代理方法  
  29. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  30. {  
  31.     NSLog(@"buttonIndex=%ld", buttonIndex);  
  32.       
  33.     // 方法1  
  34. //    if (0 == buttonIndex)  
  35. //    {  
  36. //        NSLog(@"点击了取消按钮");  
  37. //    }  
  38. //    else if (1 == buttonIndex)  
  39. //    {  
  40. //        NSLog(@"点击了确定按钮");  
  41. //    }  
  42.       
  43.     // 方法2  
  44.     NSString *title = [alertView buttonTitleAtIndex:buttonIndex];  
  45.     BOOL isEqual = [title isEqualToString:@"知道0"];  
  46.     if (isEqual)  
  47.     {  
  48.         NSLog(@"点击了确定按钮");  
  49.           
  50.         // 输入可见文本与不可见文本模式,即登录输入模式,有两个输入框  
  51.         // 第一个输入框  
  52.         UITextField *nameField = [alertView textFieldAtIndex:0];  
  53.         NSString *name = nameField.text;  
  54.         NSLog(@"name=%@",name);  
  55.           
  56.         // 第二个输入框  
  57.         UITextField *passwordField = [alertView textFieldAtIndex:1];  
  58.         NSString *password = passwordField.text;  
  59.         NSLog(@"password=%@",password);  
  60.     }  
  61.     else  
  62.     {  
  63.         NSLog(@"点击了取消按钮");  
  64.     }  
  65. }  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 方法5  
  2. /* 
  3. iOS8以后出现了UIAlertController视图控制器,通过设置UIAlertController的style属性来控制是alertview还是actionsheet 
  4. */  
  5. UIAlertController *alertContoller = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"按钮点击后我才出现的。" preferredStyle:UIAlertControllerStyleAlert];  
  6. // 响应方法-取消  
  7. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {  
  8.         NSLog(@"点击了取消按钮");  
  9. }];  
  10. // 响应方法-确定  
  11. UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {  
  12.         NSLog(@"点击了确定按钮");  
  13. }];  
  14. // 添加响应方式  
  15. [alertContoller addAction:cancelAction];  
  16. [alertContoller addAction:confirmAction];  
  17. // 显示  
  18. [self presentViewController:alertContoller animated:YES completion:nil];  





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值