简单的封装UIAlertController的两个常用样式

项目里面本来有弹框选项的工具,但是是用的UIAlertView,过期的产品总得慢慢淘汰,最近项目里面的功能经常要用到删除操作,和相片选取操作,所以得弄个选项框工具,方便开发,因为时间比较赶,所以随便先封装了两个最近特长用到的两个样式

1,一种是从底部弹出选项提示框,可以传入两个选项,其中默认有了"取消"一项,因为拍照是有"拍照"和'从相册选"两个选择,所以封装这个工具用就比较方便了

2,一种是从中间弹出的提示选框,用在是否确定删除的场景

代码比较简单,这里就不做解释了,直接复制黏贴了,以后慢慢封装,慢慢来补充,作为自己的笔记

KToast.h

//
//  KToast.h
//
//  Created by kinta on 16/9/10.
//
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

typedef void(^KAlertHandler)(UIAlertAction *action);

@interface KToast : NSObject
@property (copy, nonatomic)  KAlertHandler handler;
///添加2个选项点击,自带"取消"
+ (void)showSheetActionWithViewController:(UIViewController *)viewController title:(NSString *)title action1Title:(NSString *)title1 action2Title:(NSString *)title2 action1Handler:(KAlertHandler)handler1 action2Handler:(KAlertHandler)handler2;

///确定删除样式-中间弹框
+ (void)showDeleteDecideWithController:(UIViewController *)viewController title:(NSString *)title sureHandler:(KAlertHandler)sureHandler;
@end

 

KToast.m

//
//  KToast.m
//
//  Created by kinta on 16/9/10.
//

#import "KToast.h"

@implementation KToast

#pragma mark - UIAlertController 弹框

//MARK: 底部弹框2选项
+ (void)showSheetActionWithViewController:(UIViewController *)viewController title:(NSString *)title action1Title:(NSString *)title1 action2Title:(NSString *)title2 action1Handler:(KAlertHandler)handler1 action2Handler:(KAlertHandler)handler2;
{

    [[KToast alloc] initWithSheetActionWithViewController:viewController title:title action1Title:title1 action2Title:title2 action1Handler:handler1 action2Handler:handler2];

}
- (void)initWithSheetActionWithViewController:(UIViewController *)viewController title:(NSString *)title action1Title:(NSString *)title1 action2Title:(NSString *)title2 action1Handler:(KAlertHandler)handler1 action2Handler:(KAlertHandler)handler2;
{
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
        // Cancel button tappped.
        [viewController dismissViewControllerAnimated:YES completion:^{
        }];
    }]];
    
    [actionSheet addAction:[UIAlertAction actionWithTitle:title1 style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        !handler1 ? : handler1(action);
    }]];
    
    [actionSheet addAction:[UIAlertAction actionWithTitle:title2 style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        !handler2 ? : handler2(action);
        
    }]];
    
    [viewController presentViewController:actionSheet animated:YES completion:nil];
}
//MARK: 是否删除 中间弹框
+ (void)showDeleteDecideWithController:(UIViewController *)viewController title:(NSString *)title sureHandler:(KAlertHandler)sureHandler
{

    [[KToast alloc] initWithDeleteDecideWithController:viewController title:title sureHandler:sureHandler];
}
- (void)initWithDeleteDecideWithController:(UIViewController *)viewController title:(NSString *)title sureHandler:(KAlertHandler)sureHandler
{
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionDone = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        //删除
        !sureHandler ? : sureHandler(action);
    }];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVc addAction:actionDone];
    [alertVc addAction:actionCancel];
    [viewController presentViewController:alertVc animated:YES completion:^{
        
    }];
}
@end

 

使用就超级简单了

[KToast showSheetActionWithViewController:self title:@"请选择拍照方式" action1Title:@"拍照" action2Title:@"从相册选" action1Handler:^(UIAlertAction *action){
        //拍照操作
        
    } action2Handler:^(UIAlertAction *action){
        //从相册选操作
        
    }];
[KToast showDeleteDecideWithController:self title:@"确定删除宝贝?" sureHandler:^(UIAlertAction *action) {
        //删除操作
    }];

 

 

posted @ 2016-09-11 10:22  御龙家园  阅读(1219)  评论(0编辑  收藏  举报