您的位置:首页 > 产品设计 > UI/UE

AlertView 和 UIAlertController 简析学习和简单示例

2015-10-17 12:24 465 查看
AlertView:就是在iOS 中作为弹窗的一个控件, 例如, 登陆成功/失败 弹框提示, 输入有误弹框提示等.

UIAlertController : 是 iOS 8 出现的弹窗的控件, 功能更加强大,使用方便与否就看各位看官习惯而定,在此不做评论, 官方推荐使用这个控件作为弹窗使用, 下面也将用一个例子介绍以及简单使用

首先介绍 UIAlertView 新建一个工程 然后….

// 遵守AlertView 协议
@interface ViewController ()<UIAlertViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

// 以点击登陆 弹窗登陆成功为例
// 创建一个button
UIButton * loginButton = [UIButton buttonWithType:(UIButtonTypeSystem)];

// 标识button的大小
loginButton.frame = CGRectMake(100, 100, 100, 100);

// 设置背景颜色
loginButton.backgroundColor = [UIColor orangeColor];

// 添加button 显示文字
[loginButton setTitle:@"登陆" forState:(UIControlStateNormal)];

// 添加点击事件 并设置触发方式
[loginButton addTarget:self action:@selector(loginButtonAction) forControlEvents:(UIControlEventTouchUpInside)];

// 添加button到页面中
[self.view addSubview:loginButton];

}

// 实现 loginButton 点击方法
- (void)loginButtonAction
{

#pragma mark -- 核心代码 -- 只需要这两行代码即可运行 -- 添加button方法 目地是演示一下基础用法

// 第一种弹框方式  UIAlertView
// 自定义弹框 UIAlertView   以登陆成功为例
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"登陆成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];

// 添加AlertView  一定要有这一行
[alertView show];

}
// 实现代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// 判断是 确定 或者 取消 按钮
if (buttonIndex == 0) {
// 由于电脑计数从0 开始 则第一个按钮数字为0
NSLog(@"点击确定按钮, 为第%ld个按钮", buttonIndex);
} else {
// 由于只有两个选项 另一个按钮数字为1  也就是取消按钮
NSLog(@"点击取消按钮, 为第%ld个按钮", buttonIndex);
}

}


下面使用一个简单里例子来介绍UIAlertController 控件

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

// 设置背景颜色 为白色
self.view.backgroundColor = [UIColor whiteColor];

// 以点击登陆 弹窗登陆成功为例
// 创建一个button
UIButton * loginButton = [UIButton buttonWithType:(UIButtonTypeSystem)];

// 标识button的大小
loginButton.frame = CGRectMake(100, 100, 100, 100);

// 设置背景颜色
loginButton.backgroundColor = [UIColor orangeColor];

// 添加button 显示文字
[loginButton setTitle:@"祖国祝福语" forState:(UIControlStateNormal)];

// 添加点击事件 并设置触发方式
[loginButton addTarget:self action:@selector(loginButtonAction) forControlEvents:(UIControlEventTouchUpInside)];

// 添加button到页面中
[self.view addSubview:loginButton];

}

// 实现 loginButton 点击方法
- (void)loginButtonAction
{

#pragma mark -- 核心代码 --  由于使用controller 形式,所以使用方式和老的AlertView 不同 --

// 第二种方式  UIAlertController
// 自定义 UIAlertController
UIAlertController * alertAC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请写下您的祝福语" preferredStyle:(UIAlertControllerStyleAlert)];

// 创建确定按钮
// handler: 处理点击事件 用 block 回调
UIAlertAction * sureAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {

// 处理点击事件
// 取出添加的文本框
UITextField * textField = alertAC.textFields.firstObject;
NSLog(@"%@", textField.text);

// 取出按钮
UIAlertAction * textAction = alertAC.actions.firstObject;
NSLog(@"%@", textAction);

}];

// 将按钮添加到 alertAC 上
[alertAC addAction:sureAction];

// 创建取消按钮
UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction *action) {

// 处理点击事件
// 取出点击按钮 并输出在控制台上
UIAlertAction * textAction = alertAC.actions.lastObject;
NSLog(@"%@", textAction);

}];
// 将按钮添加到 alertVC 上
[alertAC addAction:cancelAction];

// 还可以添加通知信息文本框
[alertAC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"爱我大中华!";
textField.textColor = [UIColor redColor];
}];

// 将按钮 加载屏幕上 相当于 alertView  中的 show
[self presentViewController:alertAC animated:YES completion:nil];

}


简单介绍到此结束, 多谢各位看官捧场…

【本文由“0奋斗途中0”发布,2017年5月10日】
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 控件