UIViewController POP回调

//类别默认遵循其协议
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN
typedef void(^popHandleBlock)(UIViewController *currentCtrl);
@interface UIViewController (PopHandle)
//设置当前controller的Navigation代理
- (void)setNavigationControllerDelegate:(id<UINavigationControllerDelegate>)delegate popHandle:(popHandleBlock)popHandle;
@end

NS_ASSUME_NONNULL_END

#import "UIViewController+PopHandle.h"
#import <objc/runtime.h>
@interface UIViewController()
@property (nonatomic,copy)popHandleBlock popAfterHandle;
@property (nonatomic,assign)NSInteger subControllerCount;
@property (nonatomic,assign)bool isFinish;
@end

@implementation UIViewController (PopHandle)

- (void)setNavigationControllerPopHandle:(popHandleBlock)popHandle
{
    self.navigationController.delegate = self;
    if(popHandle) self.popAfterHandle = popHandle;
}
- (void)setNavigationControllerDelegate:(id<UINavigationControllerDelegate>)delegate popHandle:(popHandleBlock)popHandle
{
    self.navigationController.delegate = delegate;
    if(popHandle) self.popAfterHandle = popHandle;
}
//
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    NSInteger index = [navigationController.viewControllers indexOfObject:viewController];
    if(![navigationController.viewControllers.lastObject isEqual:self] && index < self.subControllerCount){
        if(self.popAfterHandle && NO == self.isFinish){
            self.popAfterHandle(navigationController);
            self.isFinish = YES;
        }
    }
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if(self.subControllerCount == 0){
        self.subControllerCount = navigationController.viewControllers.count;
    }
}
- (void)setPopAfterHandle:(popHandleBlock)popAfterHandle
{
    if(popAfterHandle){
        objc_setAssociatedObject(self, @selector(popAfterHandle), popAfterHandle, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
}
- (popHandleBlock)popAfterHandle
{
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setSubControllerCount:(NSInteger)subControllerCount
{
    objc_setAssociatedObject(self, @selector(subControllerCount), @(subControllerCount), OBJC_ASSOCIATION_ASSIGN);
}
- (NSInteger)subControllerCount
{
    return [objc_getAssociatedObject(self, _cmd) integerValue];
}
//
- (void)setIsFinish:(bool)isFinish
{
    objc_setAssociatedObject(self, @selector(isFinish), @(isFinish), OBJC_ASSOCIATION_ASSIGN);
}
- (bool)isFinish
{
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

@end
    //使用,这里的self是ViewController
    [self setNavigationControllerDelegate:self popHandle:^(UIViewController * _Nonnull currentCtrl) {
        NSLog(@"%@",currentCtrl);
    }];

封装的一个UIButton,主要是思想:

#import <UIKit/UIKit.h>
@interface UIButton (Block)
- (void)eventWithBlock:(void(^)(id sender))block;
@end
//
#import "UIButton+Block.h"
#import <objc/runtime.h>

@implementation UIButton (Block)
- (void)eventWithBlock:(void(^)(id sender))block
{
    if(block){
        //等同于自动为当前button绑定了一个成员变量,也就是block
        objc_setAssociatedObject(self, "this is button block key", block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    [self addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)btnAction:(id)sender
{
    //获取当前block
    void(^btBlock)(id sender) =  objc_getAssociatedObject(self, "this is button block key");
    if(btBlock) btBlock(sender);
}

@end

Leave a Reply

Required fields are marked *