iOS 提示框

#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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值