美文网首页
OC-循环push控制器 限制其出现的数量

OC-循环push控制器 限制其出现的数量

作者: SK丿希望 | 来源:发表于2019-11-08 15:57 被阅读0次
    原理:

    主要是对UIViewController进行添加limitNumber属性,在set方法中进行重新self.navigationController.childViewControllers的重新排序

    使用方法 :
    // 只需要在当前控制器中设置限制数量即可
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.limitNumber = 2;
    }
    
    打印输出

    也可以这样:

    #import "OneViewController.h"
    #import "UIViewController+HWCategory.h"
    
    @interface OneViewController ()
    
    @end
    
    @implementation OneViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.limitNumber = 1;
    }
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        OneViewController *vc = [OneViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    @end
    
    自己push自己
    代码
    .h文件
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface UIViewController (HWCategory)
    /** 运行push出现的限制数量 */
    @property(nonatomic, assign) NSUInteger limitNumber;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    .m文件
    #import "UIViewController+HWCategory.h"
    #import <objc/runtime.h>
    
    static NSString *const hw_pushlimitNumberKey = @"hw_pushlimitNumberKey";
    
    @implementation UIViewController (HWCategory)
    
    - (void)setLimitNumber:(NSUInteger)limitNumber {
        NSString *number = [NSString stringWithFormat:@"%ld", limitNumber];
        objc_setAssociatedObject(self, (__bridge const void * _Nonnull)(hw_pushlimitNumberKey), number, OBJC_ASSOCIATION_ASSIGN);
        [self controllerSort];
    }
    - (NSUInteger)limitNumber {
        NSString *maxNumber = objc_getAssociatedObject(self, (__bridge const void * _Nonnull)(hw_pushlimitNumberKey));
        return [maxNumber integerValue];
    }
    
    - (void)controllerSort {
        NSUInteger limitNum = self.limitNumber;
        NSArray *vcs = self.navigationController.childViewControllers;
        if (limitNum < 1) {return;}
        if (vcs.count <= limitNum) {return;}
        NSMutableArray *newvcs = [vcs mutableCopy];
        NSMutableArray *indexs = [NSMutableArray array]; // 记录存在角标
        for(int i = 0; i < vcs.count; i++){
            UIViewController *vc = vcs[i];
            if ([vc isKindOfClass:[self class]]) {
                [indexs addObject:@(i)];
            }
        }
        if (indexs.count > limitNum) { // 如果存在数量大于限制的 需要进行排序
            for(int i = 0; i < indexs.count-limitNum; i++){ // 移除最前面的
                NSInteger index = [indexs[i] integerValue];
                [newvcs removeObjectAtIndex:index];
            }
            NSLog(@"正常\n:%@\n", vcs);
            NSLog(@"排序后\n:%@\n", newvcs);
            [self.navigationController setViewControllers:newvcs animated:YES];
        }
    }
    
    @end
    

    Dome

    相关文章

      网友评论

          本文标题:OC-循环push控制器 限制其出现的数量

          本文链接:https://www.haomeiwen.com/subject/ahqwbctx.html