zoukankan      html  css  js  c++  java
  • iOS UI-AlertView(警示框)和ActionSheet(选择框、操作表单)

      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     // 创建展示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<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
  • 相关阅读:
    大学生创业不可或缺的六项品质
    C#的9*9乘法表!
    湖北武汉的进来!看看!
    每束焰火都装了电脑芯片
    学习C#之旅 魔泡排序
    学习C#之旅(C#语言基础,运算符)
    主攻ASP.NET.3.5.MVC架构之重生: URL Routing (三)
    主攻ASP.NET.3.5.MVC架构之重生: LINQ(六)
    【HDU】3341 Lost's revenge
    【HDU】2243 考研路茫茫――单词情结
  • 原文地址:https://www.cnblogs.com/oc-bowen/p/5088539.html
Copyright © 2011-2022 走看看