iOS 提示框

来源:互联网 发布:linux wifi破解 编辑:程序博客网 时间:2024/06/05 05:45

#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 = [UIAlertControlleralertControllerWithTitle:@"提示"message:message preferredStyle:UIAlertControllerStyleAlert];

    

    UIAlertAction *otherAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *action) {

        if (confirm) {

            confirm(action);

        }

    }];

    

    // Add the actions.

    [alertController addAction:otherAction];

    

    [target presentViewController:alertControlleranimated:YEScompletion:nil];

    

    


}



+(void)alertWithTarget:(id)target andMessage:(NSString *)message andConfirm:(ConfirmBlock)confirm andCancel:(CancelBlock)cancel{

     UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"提示"message:message preferredStyle:UIAlertControllerStyleAlert];

    

    UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *action) {

        if (cancel) {

            cancel(action);

        }

    }];

    

    UIAlertAction *otherAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *action) {

        if (confirm) {

            confirm(action);

        }

    }];

    

    // Add the actions.

    [alertController addAction:cancelAction];

    [alertController addAction:otherAction];

    

    [target presentViewController:alertControlleranimated:YEScompletion:nil];

    

    

    

}

+(void)alertTextFieldWithTarget:(id)target andMessage:(NSString *)message andConfirm:(ConfirmTextFieldBlock)confirm andCancel:(CancelBlock)cancel{

    UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"消息"message:message preferredStyle:UIAlertControllerStyleAlert];

   

    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

        textField.placeholder =@"请输入信息";

    }];


    UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *action) {

        if (cancel) {

            cancel(action);

        }

    }];

    

    UIAlertAction *otherAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *action) {

        UITextField *textField = alertController.textFields.firstObject;

        if (confirm) {

            confirm(action,textField.text);

        }

    }];

    

    

    // Add the actions.

    [alertController addAction:cancelAction];

    [alertController addAction:otherAction];

    

    [target presentViewController:alertControlleranimated:YEScompletion:nil];

    

    

    

}




+(void)sheetWithTarget:(id)target andView:(UITableViewCell *)cell andCamera:(CameraBlock)camera andPhoto:(PhotoBlock)photo{

    UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"选择图片来源"message:nilpreferredStyle:UIAlertControllerStyleActionSheet];

    

    

    UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *action) {

        

    }];

    

    UIAlertAction *cameraAction = [UIAlertActionactionWithTitle:@"拍照"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *action) {

        if (camera) {

            camera(action);

        }

    }];

    

     [cameraAction setValue:[UIColorcolorWithRed:0 green:0.48blue:1 alpha:1]forKey:@"_titleTextColor"];

    UIAlertAction *photoAction = [UIAlertActionactionWithTitle:@"相册"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *action) {

        if (photo) {

            photo(action);

        }

    }];

   [photoAction setValue:[UIColorcolorWithRed:0 green:0.48blue: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:alertControlleranimated:YEScompletion:nil];

}



+(void)alertWithTitle:(NSString *)title {

    UIFont *font ;

    if ([[UIDevicecurrentDevice]userInterfaceIdiom] ==UIUserInterfaceIdiomPad) {

        font = [UIFontsystemFontOfSize:11.0/375*ScreenWidth];

    }else{

        font = [UIFontsystemFontOfSize:16.0/375*ScreenWidth];

    }

    

    CGRect tmpRect = [titleboundingRectWithSize:CGSizeMake(1000, 20.0/375*ScreenWidth)options:NSStringDrawingUsesLineFragmentOriginattributes:[NSDictionarydictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil] context:nil];

    CGFloat width = tmpRect.size.width;

    

    

    UIWindow  *windows = [UIApplicationsharedApplication].keyWindow;

    UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(ScreenWidth/2 - (width +40)/2,ScreenHeight - 40 - 80, width +40, 40)];

    view.backgroundColor = [UIColorgrayColor];

    view.layer.masksToBounds =YES;

    view.layer.cornerRadius = 20;

    UILabel *label = [[UILabelalloc]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 = [UIColorwhiteColor];

    [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
原创粉丝点击