iOS开发中弹窗的方式

由于iOS系统的不断更新, 所以导致很多就方法慢慢的被淘汰,但是在一些旧的项目中仍然在使用, 这就到时很多初学者容易对新旧方法混淆不清, 下面介绍一下关于弹窗的新旧不同方式及其使用方法, 废话不多说,  上代码
//  ViewController.m
//  弹窗的四中种形式
//
//  Created by Wangjunling on 16/3/9.
//  Copyright © 2016年 Wangjunling. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <UIAlertViewDelegate, UIActionSheetDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    //[self test1];// iOS8之前屏幕中间弹窗
//    [self test2];// iOS8之前屏幕底部弹窗
//    [self test3];// iOS8之后屏幕中间弹窗
    [self test4];// iOS8之后屏幕底部弹窗
}

#pragma mark 方法1 iOS8之前的弹窗方式,在屏幕中间弹窗
- (void)test1{
    // 1.创建一个中间弹框,有“取消”和“确定按钮”,设置代理为当前控制器,由控制器监听点击了“取消”还是“确定”按钮
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"点击了屏幕" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    //2显示
    [alert show];
}
#pragma mark UIAlertViewDelegate 代理方法, 监听方法1中出现的弹框中的按钮点击,控制器来监听点击了取消还是确定按钮
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    // 3处理点击事件
    if (buttonIndex == 0) {
        NSLog(@"点击了取消按钮");
    } else {
        NSLog(@"点击了确定按钮");
    }
}
#pragma mark 方法2 iOS8之前的弹窗方式,在屏幕底部弹窗
- (void)test2 {
    //1创建底部弹窗
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"点击了屏幕" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确认" otherButtonTitles:nil, nil];
    //2显示
    [sheet showInView:self.view];
}
#pragma mark 实现actionsheet 代理
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    //3处理点击事件
    if (buttonIndex == 0) {
        NSLog(@"点击了取消按钮");
    } else {
        NSLog(@"点击了确定按钮");
    }
}

#pragma mark 方法3 用在IOS8以后,没有代理。点击按钮时要执行的操作放在了block中,因此不需要设置代理
- (void)test3{
    // 1.创建弹框控制器, UIAlertControllerStyleAlert这个样式代表弹框显示在屏幕中央
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"点击了屏幕" preferredStyle:UIAlertControllerStyleAlert];
    // 2.添加取消按钮,block中存放点击了“取消”按钮要执行的操作
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"点击了取消按钮");
    }];
    
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        NSLog(@"点击了确定按钮");
        
    }];
    // 3.将“取消”和“确定”按钮加入到弹框控制器中
    [alertVc addAction:cancle];
    [alertVc addAction:confirm];

    // 4.控制器 展示弹框控件,完成时不做操作
    
    [self presentViewController:alertVc animated:YES completion:^{
        
        nil;
        
    }];
    
}
#pragma mark 方法4用在IOS8以后,弹框的样式变为“UIAlertControllerStyleActionSheet”, 弹框出现在屏幕底部
- (void)test4 {
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"点击了屏幕" preferredStyle:UIAlertControllerStyleActionSheet];
   
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"点击了取消");
    }];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSLog(@"点击了确定");
    }];
    /*关于UIAlertActionStyle有三种样式 
     UIAlertActionStyleDefault  , 默认样式
     UIAlertActionStyleCancel,      取消
     UIAlertActionStyleDestructive, 有毁灭性的操作是使用, 呈现红色
     */
    [alertVc addAction:cancle];
    [alertVc addAction:confirm];
    
    [self presentViewController:alertVc animated:YES completion:^{
        
        nil;
        
    }];
    
}
@end

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值