zoukankan      html  css  js  c++  java
  • UIAlertController的使用

     1 //
     2 //  RootViewController.m
     3 //  AlertControllerShare
     4 //
     5 //  Created by  on 16/3/5.
     6 //  Copyright (c) 2016年 Liuf. All rights reserved.
     7 //
     8 
     9 #import "RootViewController.h"
    10 
    11 @interface RootViewController ()
    12 
    13 @end
    14 
    15 @implementation RootViewController
    16 
    17 - (void)viewDidLoad {
    18     [super viewDidLoad];
    19     [self setButton];
    20 }
    21 
    22 //创建一个Button,用于触发Alert事件
    23 - (void)setButton {
    24     //1.创建一个Button对象
    25     UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    26     btn.frame = CGRectMake(145, 100, 60, 35);
    27     
    28     //2.设置Button属性
    29     [btn setTitle:@"测试" forState:UIControlStateNormal];
    30     [btn addTarget:self action:@selector(testAlertBtnAction:) forControlEvents:UIControlEventTouchDown];
    31     
    32     //3.给Button按钮设置边框,这里不是必须的
    33     [btn.layer setMasksToBounds:YES];   //设置按钮的圆角半径不会被遮挡
    34     [btn.layer setCornerRadius:10];
    35     [btn.layer setBorderWidth:2];
    36     [btn.layer setBorderColor:[UIColor blueColor].CGColor];  //配置边框颜色
    37     
    38     [self.view addSubview:btn];  //将按钮添加到视图
    39 }
    40 
    41 //实现Button的点击事件
    42 - (void)testAlertBtnAction:(UIButton *)sender {
    43     [self configAlert];
    44 }
    45 
    46 //配置Alert为对话框式弹框
    47 - (void)configAlert {
    48     
    49     //1.创建AlertController对象
    50     //参数3设置为 UIAlertControllerStyleActionSheet 时,是底部弹框模式
    51     UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"测试" message:@"这是对话框模式" preferredStyle:UIAlertControllerStyleAlert];
    52     //2.设置alert按钮活动
    53     UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    54   //其中handle之后的参数是使用了Block,在这里可以写一些点击按钮之后的处理事件
    55         NSLog(@"你点击了取消");
    56     }];
    57     UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    58         NSLog(@"你点击了确定");
    59      
    60         UITextField *login = alert.textFields[0];  //alert.textFields会获取对话框中的全部文本框,并返回一个数组
    61         UITextField *password = alert.textFields[1];
    62         
    63         NSLog(@"%@,%@", login.text, password.text);
    64     }];
    65     //UIAlertActionStyleDestructive风格,会以红色文字显示,多用于提示可能造成问题的地方,实际使用与Default风格无差别
    66     UIAlertAction *delete = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
    67         NSLog(@"你点击了删除");
    68     }];
    69     //3.根据需要设置文本框,只有在对话框模式下可以设置文本框,底部弹框模式不可用
    70     [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    71         textField.placeholder = @"输入账号";    //设置文本框的提示符
    72     }];
    73     [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    74         textField.placeholder = @"输入密码";
    75         textField.secureTextEntry = YES;    //密码输入设置为密文模式
    76     }];
    77     //4.将活动添加到控制器
    78     [alert addAction:cancel];
    79     [alert addAction:ok];
    80     [alert addAction:delete];
    81     //5.显示警示框
    82     [self presentViewController:alert animated:YES completion:nil];
    83 }
    84 
    85 
    86 - (void)didReceiveMemoryWarning {
    87     [super didReceiveMemoryWarning];
    88 }
    89 
    90 @end
    91 
    92  

    UIAlertController是在iOS8之后推荐使用的,我这里就不再过多的讲解,直接贴出代码,供大家参考使用。。

    另外,UIButton可能大家都会用,但是我也接触一些人,不太会给Button设置一个边框,我在代码中也特意把这一块给写了出来,由需要的朋友可以参考一下。

    转载请注明出处
  • 相关阅读:
    vfs:结构体对象
    vfs:open.c 源码学习
    Linux direct io使用例子
    GPU安装小结
    tensorflow scope的作用
    tensorflow 一维卷积 tf.layers.conv1()使用
    tensorflow 的tf.where详解
    tensorflow 的tf.split函数的用法
    tensorflow 添加一个全连接层
    tensorflow 计算均值和方差
  • 原文地址:https://www.cnblogs.com/zzuliliu/p/5245587.html
Copyright © 2011-2022 走看看