//
// ZFSAlertView.h
// test
//
// Created by HandsomeC on 2017/12/11.
// Copyright © 2017年 趙發生. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger,ZFSAlertViewStyle) {
ZFSAlertViewAlert = 0,//中間彈框
ZFSAlertViewSheet,//底部彈框
};
typedef void (^clickBlock)(NSInteger itemIndex);
@interface ZFSAlertView : UIView
/*
調用的時候回調實在閉包裏面,請注意避免循環引用
*/
/**
彈框封裝
@param title 標題
@param message 信息
@param cancelTitle 取消按鈕文字
@param actionsArr 按鈕數組
@param style 彈框類型(中間或者是底部)
@param controller 彈出視圖的父視圖
@param itemBlock 回調
*/
+ (void)AlertViewWithTitle:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle acitons:(NSArray *)actionsArr style:(ZFSAlertViewStyle)style inView:(UIViewController*)controller itemblock:(clickBlock)itemBlock;
@end
//
// ZFSAlertView.m
// test
//
// Created by HandsomeC on 2017/12/11.
// Copyright © 2017年 趙發生. All rights reserved.
//
#import "ZFSAlertView.h"
@implementation ZFSAlertView
/*
回調爲零默認爲取消按鈕
*/
+ (void)AlertViewWithTitle:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle acitons:(NSArray *)actionsArr style:(ZFSAlertViewStyle)style inView:(UIViewController*)controller itemblock:(clickBlock)itemBlock{
UIAlertControllerStyle alertStyle;
if (!style) {
alertStyle = UIAlertControllerStyleAlert;
}else{
alertStyle = UIAlertControllerStyleActionSheet;
}
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:alertStyle];
[alertController addAction:[UIAlertAction actionWithTitle:cancelTitle.length > 0? cancelTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
itemBlock(0);//取消返回0值
}]];
for (NSInteger i = 0; i < actionsArr.count; i++) {
[alertController addAction:[UIAlertAction actionWithTitle:actionsArr[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
itemBlock(i+1);
}]];
}
[controller presentViewController:alertController animated:YES completion:nil];
}
@end