您的位置:首页 > 移动开发 > IOS开发

IOS开发中的弹框综述

2016-09-14 00:42 316 查看
题记————

人生在世,淡定看世界。

感受生活,从不说疲惫,因为卡里的存款还不够多。

体验生活,从不说难受,因为肩上的责任不能丢。

感悟生活,从不说后退,因为脚下的路依然要走

1、预览



2、弹出框的基本实现方式

2-1 UIAlertView

2-2 UIAlertController

3、AlertView弹出方式简述

3-1 实现方式

//AlertView中央弹出
- (IBAction)AlertViewBottomShow:(id)sender {
//创建
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"从底部弹出" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
//添加代理
alertView.delegate = self;
//显示
[alertView show];
}


显示效果:



可以看到在这里同时也设置了代理,只是为了监听弹出框的点击事件 ,当然同时要实现协议(或者称为遵守)

#import "ViewController.h"

@interface ViewController () <UIAlertViewDelegate>

@end


同时复写代理 监听回调方法

//alertView 代理回调
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"%lu",buttonIndex);
}
那么在这里就可以监听到对应的点击按钮,就可以做相应的操作

4、UIAlertController弹出方式

4-1 视图中央弹出



实现方式:

//显示方式为直接从视图中央弹出
- (IBAction)clickUIAlertControllerFunction:(id)sender {
//创建
UIAlertController *controll = [UIAlertController alertControllerWithTitle:@"提示" message:@"点击监听提示" preferredStyle:UIAlertControllerStyleAlert];
//添加取消按钮 并设置Block
[controll addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"CLICK 取消");
}]];
//添确认消按钮 并设置Block
[controll addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"CLICK 确认");
}]];
//弹出
[self presentViewController:controll animated:NO completion:nil];
}


可以看到在添加按钮的时候同时添加了一个对应的block函数,来监听对应的按钮的点击事件

4-2 、从视图的底部弹出显示框



实现:

//AlertController底部弹出
- (IBAction)AlertControllerBottomShow:(id)sender {
//创建
UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"提示" message:@"底部弹出" preferredStyle:UIAlertControllerStyleActionSheet];
//添加按钮一
[controller addAction:[UIAlertAction actionWithTitle:@"选择一" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"CLICK 选择一");
}]];
//添加按钮一
[controller addAction:[UIAlertAction actionWithTitle:@"选择二" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"CLICK 选择二");
}]];
//添加按钮三
[controller addAction:[UIAlertAction actionWithTitle:@"选择三" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"CLICK 选择三");
}]];
//显示
[self presentViewController:controller animated:YES completion:nil];
}


与方式一相比较,基本实现方式是一致的,不同的是在创建View的时候,设置的弹出方式preferredStyle不同 ,默认的方式是从视图中间显示,设置弹出方式为UIAlertControllerollerStyleActionSheet,则实现了弹框从底部弹出的效果

对应按钮的点击事件则在设置的对应的Block函数中实现

5、向弹出框中添加输入框



//创建
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"文本对话框" message:@"登录和密码对话框示例" preferredStyle:UIAlertControllerStyleAlert];
//向视图中添加第一个输入框
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"用户名";
}];
//向视图中添加第二个输入框
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"电话";
textField.secureTextEntry = YES;
}];

//确认
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//获取第一个输入框
UITextField *login = alertController.textFields.firstObject;
//获取第二个输入框
UITextField *password = alertController.textFields.lastObject;

NSLog(@"%@%@",login.text,password.text);

}];

//取消
UIAlertAction *canleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

}];
//添加Action
[alertController addAction:okAction];
[alertController addAction:canleAction];
//弹出
[self presentViewController:alertController animated:YES completion:nil];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息