博客> UIAlertView和UIActionSheet的代码压缩
UIAlertView和UIActionSheet的代码压缩
2019-06-15 22:52 评论:0 阅读:357 anpengtao_ios
UIAlertView UIActionSheet

思路:

1.利用runtime的内联机制,通过objc_setAssociatedObject可以给二个对象之间增加联系,实现setter和getter方法

2.利用block的回调,在Tools中实现代理方法

注意事项:

1.ios8之后使用UIAlertController,之前使用UIAlertView 2.使用UIAlertController,在iPhone中使用

```[viewController presentViewController:alertVC animated:YES completion:nil]; 在iPad中 UIPopoverPresentationController *popPresenter = [alertVC popoverPresentationController]; popPresenter.sourceView = viewController.view; // 这就是挂靠的对象 popPresenter.sourceRect = CGRectMake(kScreenWidth/2.0, kScreenHeight-60, 1.0, 1.0); popPresenter.permittedArrowDirections = UIPopoverArrowDirectionDown; [viewController presentViewController:alertVC animated:YES completion:nil];


```#import <Foundation>

@interface TipAlertViewTool : NSObject

/**
 *  回调block
 */
typedef void (^CallBackBlock)(NSInteger btnIndex);

/**
 alertView

 @param viewController 当前VC
 @param title 标题
 @param message 说明
 @param block 回调
 @param cancelBtnTitle 关闭按钮
 @param okBtnTitle 确认按钮
 */
+ (void)alertViewConfigWith:(UIViewController *)viewController
                      title:(NSString *)title
                    message:(NSString *)message
              callbackBlock:(CallBackBlock)block
          cancelButtonTitle:(NSString *)cancelBtnTitle
              okButtonTitle:(NSString *)okBtnTitle  ;

/**
 actionSheet

 @param viewController  当前VC
 @param title 标题
 @param message 说明
 @param block 点击的回调
 @param cancelBtnTitle 取消按钮
 @param optionsArray 操作按钮数组
 */
+ (void)actionSheetViewConfigWith:(UIViewController *)viewController
                            title:(NSString *)title
                          message:(NSString *)message
                    callbackBlock:(CallBackBlock)block
                cancelButtonTitle:(NSString *)cancelBtnTitle
                     optionsTitle:(NSArray  *)optionsArray;

@end

```#import "TipAlertViewTool.h"

//判断设备是否为iPhone

define KDevice_Is_iPhone (([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) ? YES : NO )

define IS_IOS8 ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0)

pragma mark - UIAlertView类别扩充

//类别,扩充属性 @interface UIAlertView (TipAlertViewTool)

@property (nonatomic, copy) CallBackBlock clickBlock;

@end //runtime内联机制,实现set/get方法 @implementation UIAlertView (JXTAlertTools)

  • (void)setClickBlock:(CallBackBlock)block { objc_setAssociatedObject(self, @selector(clickBlock), block, OBJC_ASSOCIATION_COPY); }
  • (CallBackBlock)clickBlock { return objc_getAssociatedObject(self, @selector(clickBlock)); } @end

pragma mark - UIActionSheet类别扩充

//类别,扩充属性 @interface UIActionSheet (JXTAlertTools) @property (nonatomic, copy) CallBackBlock clickBlock; @end

//runtime内联机制,实现set/get方法 @implementation UIActionSheet (JXTAlertTools)

  • (void)setClickBlock:(CallBackBlock)block { objc_setAssociatedObject(self, @selector(clickBlock), block, OBJC_ASSOCIATION_COPY); }
  • (CallBackBlock)clickBlock { return objc_getAssociatedObject(self, @selector(clickBlock)); } @end

@interface TipAlertViewTool ()

@end

@implementation TipAlertViewTool

pragma mark - 普通alert初始化 兼容适配

  • (void)alertViewConfigWith:(UIViewController )viewController title:(NSString )title message:(NSString )message callbackBlock:(CallBackBlock)block cancelButtonTitle:(NSString )cancelBtnTitle okButtonTitle:(NSString )okBtnTitle { if (IS_IOS8) { UIAlertController alertVC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; //取消按钮 if (cancelBtnTitle.length > 0) { UIAlertAction cancelAction = [UIAlertAction actionWithTitle:cancelBtnTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction action) { block(0); }]; [alertVC addAction:cancelAction]; } //确认按钮 if (okBtnTitle.length > 0) { UIAlertAction okAction = [UIAlertAction actionWithTitle:okBtnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction action) { block(1); }]; [alertVC addAction:okAction]; } if (KDevice_Is_iPhone) { [viewController presentViewController:alertVC animated:YES completion:nil]; } else { UIPopoverPresentationController *popPresenter = [alertVC popoverPresentationController]; popPresenter.sourceView = viewController.view; // 这就是挂靠的对象 popPresenter.sourceRect = CGRectMake(kScreenWidth/2.0, kScreenHeight-60, 1.0, 1.0); popPresenter.permittedArrowDirections = UIPopoverArrowDirectionDown;

        [viewController presentViewController:alertVC animated:YES completion:nil];
    }      

    } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelBtnTitle otherButtonTitles:okBtnTitle, nil]; alert.clickBlock = block; [alert show]; } }

    pragma mark UIAlertViewDelegate 类方法才可执行回调

  • (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; { if (alertView.clickBlock) { alertView.clickBlock(buttonIndex); } }
  • (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { alertView.clickBlock = nil; }
  • (void)dismissAlertView:(UIAlertView*)alertView { [alertView dismissWithClickedButtonIndex:0 animated:YES]; }

pragma mark - 普通actionSheet初始化 兼容适配

  • (void)actionSheetViewConfigWith:(UIViewController )viewController title:(NSString )title message:(NSString )message callbackBlock:(CallBackBlock)block cancelButtonTitle:(NSString )cancelBtnTitle optionsTitle:(NSArray )optionsArray { if (IS_IOS8) { UIAlertController sheectController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet]; for (int i=0; i)self cancelButtonTitle:cancelBtnTitle destructiveButtonTitle:message otherButtonTitles: nil]; for (NSString *optString in optionsArray) { [actionSheet addButtonWithTitle:optString]; } actionSheet.clickBlock = block; [actionSheet showInView:viewController.view]; }

}

  • (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (actionSheet.clickBlock) { actionSheet.clickBlock(buttonIndex); } }
  • (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex { actionSheet.clickBlock = nil; }

@end

收藏
1
sina weixin mail 回到顶部