#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
typedef void (^ConfirmBlock)(UIAlertAction *action);
typedef void (^ConfirmTextFieldBlock)(UIAlertAction *action,NSString *text);
typedef void (^CancelBlock)(UIAlertAction *action);
typedef void (^CameraBlock)(UIAlertAction *action);
typedef void (^PhotoBlock)(UIAlertAction *action);
@interface AlertView : NSObject
+(void)alertWithTarget:(id)target andMessage:(NSString *)message andConfirm:(ConfirmBlock)confirm;
+(void)alertWithTarget:(id)target andMessage:(NSString *)message andConfirm:(ConfirmBlock)confirm andCancel:(CancelBlock)cancel;
+(void)sheetWithTarget:(id)target andView:(UIView *)view andCamera:(CameraBlock)camera andPhoto:(PhotoBlock)photo;
+(void)alertWithTitle:(NSString *)title;
+(void)alertTextFieldWithTarget:(id)target andMessage:(NSString *)message andConfirm:(ConfirmTextFieldBlock)confirm andCancel:(CancelBlock)cancel;
@end
#import "AlertView.h"
@implementation AlertView
+(void)alertWithTarget:(id)target andMessage:(NSString *)message andConfirm:(ConfirmBlock)confirm{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
if (confirm) {
confirm(action);
}
}];
// Add the actions.
[alertController addAction:otherAction];
[target presentViewController:alertController animated:YES completion:nil];
}
+(void)alertWithTarget:(id)target andMessage:(NSString *)message andConfirm:(ConfirmBlock)confirm andCancel:(CancelBlock)cancel{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
if (cancel) {
cancel(action);
}
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
if (confirm) {
confirm(action);
}
}];
// Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[target presentViewController:alertController animated:YES completion:nil];
}
+(void)alertTextFieldWithTarget:(id)target andMessage:(NSString *)message andConfirm:(ConfirmTextFieldBlock)confirm andCancel:(CancelBlock)cancel{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"消息" message:message preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"请输入信息";
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
if (cancel) {
cancel(action);
}
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
UITextField *textField = alertController.textFields.firstObject;
if (confirm) {
confirm(action,textField.text);
}
}];
// Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[target presentViewController:alertController animated:YES completion:nil];
}
+(void)sheetWithTarget:(id)target andView:(UITableViewCell *)cell andCamera:(CameraBlock)camera andPhoto:(PhotoBlock)photo{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选择图片来源" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}];
UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
if (camera) {
camera(action);
}
}];
[cameraAction setValue:[UIColor colorWithRed:0 green:0.48 blue:1 alpha:1] forKey:@"_titleTextColor"];
UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
if (photo) {
photo(action);
}
}];
[photoAction setValue:[UIColor colorWithRed:0 green:0.48 blue:1 alpha:1] forKey:@"_titleTextColor"];
[alertController addAction:cancelAction];
[alertController addAction:cameraAction];
[alertController addAction:photoAction];
if(IsPad){
UIPopoverPresentationController *popover = alertController.popoverPresentationController;
popover.sourceView = cell;
popover.sourceRect =CGRectMake(0, cell.bounds.size.height/2, ScreenWidth, 1.0);
}
[target presentViewController:alertController animated:YES completion:nil];
}
+(void)alertWithTitle:(NSString *)title {
UIFont *font ;
if ([[UIDevice currentDevice]userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
font = [UIFont systemFontOfSize:11.0/375*ScreenWidth];
}else{
font = [UIFont systemFontOfSize:16.0/375*ScreenWidth];
}
CGRect tmpRect = [title boundingRectWithSize:CGSizeMake(1000, 20.0/375*ScreenWidth) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName, nil] context:nil];
CGFloat width = tmpRect.size.width;
UIWindow *windows = [UIApplication sharedApplication].keyWindow;
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(ScreenWidth/2 - (width +40)/2,ScreenHeight - 40 - 80, width +40, 40)];
view.backgroundColor = [UIColor grayColor];
view.layer.masksToBounds = YES;
view.layer.cornerRadius = 20;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(view.frame.size.width/2 - width/2, view.frame.size.height/2 - 10.0/375*ScreenWidth, width, 20.0/375*ScreenWidth)];
label.textAlignment = 1;
label.font = font;
label.text = title;
label.textColor = [UIColor whiteColor];
[view addSubview:label];
[windows addSubview:view];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
//2秒以后移除view
[view removeFromSuperview];
});
}
@end