博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS UI-AlertView(警示框)和ActionSheet(选择框、操作表单)
阅读量:6718 次
发布时间:2019-06-25

本文共 7519 字,大约阅读时间需要 25 分钟。

1 #import "ViewController.h"  2   3 @interface ViewController ()
4 5 @end 6 7 @implementation ViewController 8 9 #pragma mark - 生命周期 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 // 创建展示AlertView的按钮 13 UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; 14 [btn setTitle:@"AlertView" forState:UIControlStateNormal]; 15 [btn setFrame:CGRectMake(130, 150, 100, 100)]; 16 //btn.center =self.view.center; 17 [btn setBackgroundColor:[UIColor orangeColor]]; 18 [btn addTarget:self action:@selector(showAlert) forControlEvents:UIControlEventTouchUpInside]; 19 20 // 创建展示ActionSheet的按钮 21 [self.view addSubview:btn]; 22 UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeSystem]; 23 [btn1 setTitle:@"ActionSheet" forState:UIControlStateNormal]; 24 [btn1 setFrame:CGRectMake(130, 300, 100, 100)]; 25 [btn1 setBackgroundColor:[UIColor redColor]]; 26 [btn1 addTarget:self action:@selector(showActionSheet) forControlEvents:UIControlEventTouchUpInside]; 27 [self.view addSubview:btn1]; 28 29 30 } 31 #pragma mark - 关联事件 32 33 #pragma mark - 警示框 34 - (void)showAlert 35 { 36 /* 37 38 UIAlertView警示框控件 39 注意 40 不用创建全局的Alert 41 尽量不要关联逻辑操作,如果关联实现协议:UIAlertViewDelegate以及里面的协议方法 42 43 */ 44 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" 45 message:@"网络连接失败" 46 delegate:self 47 cancelButtonTitle:@"好的" 48 otherButtonTitles:@"方案1",@"方案2",@"方案3", nil]; 49 50 [alert show]; 51 } 52 #pragma mark - 选择框 53 - (void)showActionSheet 54 { 55 /* 56 57 UIActionSheet 选择框 58 注意 59 不用创建全局的UIActionSheet 60 尽量关联逻辑操作,如果关联实现协议:UIActionSheetDelegate以及里面的协议方法 61 62 */ 63 64 UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"请选择你的节操充值额度" 65 delegate:self 66 cancelButtonTitle:@"算了,不要了" 67 destructiveButtonTitle:@"18.00RMB" 68 otherButtonTitles:@"19.00RMB",@"20.00RMB",@"21.00RMB", nil]; 69 70 [action showInView:self.view]; 71 } 72 73 #pragma mark - 选择框代理方法 74 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 75 { 76 switch (buttonIndex) { 77 case 0: 78 NSLog(@"第一种情况"); 79 break; 80 case 1: 81 NSLog(@"第二种情况"); 82 break; 83 case 2: 84 NSLog(@"第三种情况"); 85 break; 86 case 3: 87 NSLog(@"第四种情况"); 88 break; 89 case 4: 90 NSLog(@"第五种情况"); 91 break; 92 93 default: 94 break; 95 } 96 } 97 #pragma mark - 警示框代理方法 98 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 99 {100 switch (buttonIndex) {101 case 0:102 NSLog(@"序号为0");103 break;104 case 1:105 NSLog(@"序号为1");106 break;107 case 2:108 NSLog(@"序号为2");109 break;110 case 3:111 NSLog(@"序号为3");112 break;113 114 default:115 break;116 }117 }118 119 120 - (void)didReceiveMemoryWarning {121 [super didReceiveMemoryWarning];122 // Dispose of any resources that can be recreated.123 }124 125 @end

 

 

1 //  2 //  ViewController.m  3 //  IOS_0226_UIAlertController  4 //  5 //  Created by ma c on 16/2/26.  6 //  Copyright © 2016年 博文科技. All rights reserved.  7 //  8   9 #import "ViewController.h" 10  11 @interface ViewController () 12 @property (weak, nonatomic) IBOutlet UIButton *btnClick; 13  14 @end 15  16 @implementation ViewController 17  18 /* 19  UIPresentationController ->管理所有Modal出来的控制器 20   21  1.管理所有通过presentViewController方法显示出来的控制器 22  2.只要调用了presentViewController方法就会创建UIPresentationController 23  3.管理/监听切换控制器的过程 24  4.属性 25  //当前控制器 26  @property(nonatomic, strong, readonly) UIViewController *presentingViewController; 27  //切换的控制器 28  @property(nonatomic, strong, readonly) UIViewController *presentedViewController; 29  //切换的控制器视图 30  - (nullable UIView *)presentedView; 31   32  5.UIViewController属性 33   34  @property (nonatomic,readonly) UIPresentationController *presentationController 35  @property (nonatomic,readonly) UIPopoverPresentationController *popoverPresentationController 36   37  这两个属性指向UIPresentationController 38  39  */ 40  41 - (void)viewDidLoad { 42     [super viewDidLoad]; 43     self.view.backgroundColor = [UIColor cyanColor]; 44      45  46 } 47  48 - (void)touchesBegan:(NSSet
*)touches withEvent:(UIEvent *)event 49 { 50 //[self createAlertVC]; 51 [self createPopoverPresentationVC]; 52 } 53 54 - (void)createAlertVC 55 { 56 //警示框 57 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"好久不见,你又不乖了。" preferredStyle:UIAlertControllerStyleAlert]; 58 //添加按钮 59 UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { 60 NSLog(@"点击了确定按钮"); 61 }]; 62 63 [alert addAction:action]; 64 65 [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 66 NSLog(@"点击了取消按钮"); 67 }]]; 68 69 //添加文本框 70 [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { 71 textField.borderStyle = UITextBorderStyleRoundedRect; 72 textField.textColor = [UIColor redColor]; 73 textField.text = @"123"; 74 [textField addTarget:self action:@selector(uernameChange:) forControlEvents:UIControlEventEditingChanged]; 75 76 }]; 77 [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { 78 textField.borderStyle = UITextBorderStyleRoundedRect; 79 80 textField.secureTextEntry = YES; 81 textField.text = @"123"; 82 }]; 83 [self presentViewController:alert animated:nil completion:nil]; 84 } 85 86 - (void)uernameChange:(UITextField *)textField 87 { 88 NSLog(@"%@",textField.text); 89 } 90 91 92 - (void)createPopoverPresentationVC 93 { 94 UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择操作" preferredStyle:UIAlertControllerStyleActionSheet]; 95 //设置展示形式 96 actionSheet.modalTransitionStyle = UIModalPresentationPopover; 97 self.popoverPresentationController.sourceView = self.btnClick; 98 self.popoverPresentationController.sourceRect = self.btnClick.bounds; 99 100 101 UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {102 NSLog(@"点击了确定按钮");103 }];104 [actionSheet addAction:action];105 106 [actionSheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {107 NSLog(@"点击了取消按钮");108 }]];109 110 [self presentViewController:actionSheet animated:nil completion:nil];111 112 }113 114 115 116 @end

 

转载地址:http://tuumo.baihongyu.com/

你可能感兴趣的文章
Android多线程源码详解一:handler、looper、message、messageQueue
查看>>
wordpress robot设置
查看>>
unity3d 中控制手机前后摄像头切换
查看>>
MyCAT核心配置详解
查看>>
selenium启动Chrome配置参数问题
查看>>
刚刚,2018年度中国科学十大进展正式发布!
查看>>
为什么游戏服务端用开发效率低的C++来写,其他语言无法胜任吗?
查看>>
Java开发——Redis云管理平台 实现方案CacheCloud 扫盲
查看>>
Apache NiFi 1.9.2 发布,数据处理和分发系统
查看>>
有哪些Java源代码看了后让你收获很多?
查看>>
设置input标签placeholder字体颜色
查看>>
跳出面向对象思想(一) 继承
查看>>
01 聚类算法 - 大纲
查看>>
为什么说“上云就上阿里云”
查看>>
tomcat配置文件详解
查看>>
iOS NSURLSession DownloadTask(下载任务)
查看>>
vue解决字段类型为数字导致单选不正确的问题
查看>>
Prometheus 2.0正式推出 性能提升带来质的飞跃
查看>>
WPF实现抽屉效果
查看>>
http2-浏览器支持的情况
查看>>