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
- ios提示框汇总
- IOS之提示框
- iOS学习,提示框
- IOS提示框
- iOS-UIActionSheet提示框
- iOS 自定义提示框
- IOS-系统提示框
- iOS--自定义提示框
- iOS UIAlertController提示框
- iOS UIActionSheet提示框
- IOS弹出提示框
- iOS 提示框工具
- IOS弹出提示框
- iOS蓝牙提示框
- 关于IOS提示框
- iOS 提示框
- ios---顶部提示框
- ios提示框
- cocoa 和 cocoaTouch
- mysql数据移植过程中遇到的常见问题解析
- ORACLE使用透明数据库加密
- wxWidgets如何捕获窗口外的鼠标消息
- cpython
- iOS 提示框
- Android studio RecyclerView实现炫酷吸顶效果
- Android 从初级到资深
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
- 学习笔记:boost iostream filter封装openssl EVP
- 面向过程编程和面向对象编程
- c++中构造函数和析构函数的概念
- iOS多态:初始化时效果出不来?可能是因为覆盖了父类的同名方法
- 通过重新编译Opencv修改摄像头分辨率