iOS UI-AlertView(警示框)和ActionSheet(选择框、操作表单)
生活随笔
收集整理的這篇文章主要介紹了
iOS UI-AlertView(警示框)和ActionSheet(选择框、操作表单)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1 #import "ViewController.h"
2
3 @interface ViewController ()<UIAlertViewDelegate,UIActionSheetDelegate>
4
5 @end
6
7 @implementation ViewController
8
9 #pragma mark - 生命周期
10 - (void)viewDidLoad {
11 [super viewDidLoad];
12 // 創(chuàng)建展示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 // 創(chuàng)建展示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 - 關聯(lián)事件
32
33 #pragma mark - 警示框
34 - (void)showAlert
35 {
36 /*
37
38 UIAlertView警示框控件
39 注意
40 不用創(chuàng)建全局的Alert
41 盡量不要關聯(lián)邏輯操作,如果關聯(lián)實現(xiàn)協(xié)議:UIAlertViewDelegate以及里面的協(xié)議方法
42
43 */
44 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"錯誤"
45 message:@"網(wǎng)絡連接失敗"
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 不用創(chuàng)建全局的UIActionSheet
60 盡量關聯(lián)邏輯操作,如果關聯(lián)實現(xiàn)協(xié)議:UIActionSheetDelegate以及里面的協(xié)議方法
61
62 */
63
64 UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"請選擇你的節(jié)操充值額度"
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.只要調(diào)用了presentViewController方法就會創(chuàng)建UIPresentationController 23 3.管理/監(jiān)聽切換控制器的過程 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<UITouch *> *)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?
總結(jié)
以上是生活随笔為你收集整理的iOS UI-AlertView(警示框)和ActionSheet(选择框、操作表单)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LL-verilog 1000HZ分频为
- 下一篇: LL-verilog-HDLBitSim