封装一些导航快速操作相关的
1、pop到指定的页面
- (void)popToController:(NSString *)popController
用法:
[self.navigationController popToController:@"YQOrderViewController"];
2、替换导航栈中指定的controller
- (void)replaceObjectViewController:(Class)className withObj:(UIViewController *)object
用法:push之后替换掉当前控制器
UIViewController *AController = [[UIViewController alloc]init];
[self.navigationController pushViewController:AController animated:YES];
YQIdentifyHistoryViewController *controller = [YQIdentifyHistoryViewController new];
[self.navigationController replaceObjectViewController:self.class withObj:controller];
3、移除导航栈中指定的controller 可以 移除多个 放到数组中即可
- (void)killControllers:(NSArray<NSString *> *)killControllers
用法:
YQPaymentResultViewController *result = [[YQPaymentResultViewController alloc]init];
result.title = @"结果";
[self.navigationController pushViewController:result animated:YES];
[self.navigationController killControllers:@[self.className]];
代码实现
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UINavigationController (YQ)
- (void)replaceObjectViewController:(Class)className withObj:(UIViewController *)object ;
- (void)killControllers:(NSArray<NSString *> *)killControllers;
- (void)popToController:(NSString *)popController;
@end
NS_ASSUME_NONNULL_END
#import "UINavigationController+YQ.h"
@implementation UINavigationController (YQ)
/// 替换指定的controller
/// @param className 被替换的controller名字
/// @param object 将要替换的controller
- (void)replaceObjectViewController:(Class)className withObj:(UIViewController *)object {
NSMutableArray *controllers = self.viewControllers.mutableCopy;
@weakify(controllers);
[controllers enumerateObjectsWithOptions:(NSEnumerationConcurrent)
usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:className]) {
@strongify(controllers);
*stop = YES;
[controllers replaceObjectAtIndex:idx withObject:object];
dispatch_async(dispatch_get_main_queue(), ^{
[self configuration:object];
});
}
}];
self.viewControllers = controllers;
}
/// 杀掉导航栈中值指定的controller
/// @param killControllers 被杀的controllers
- (void)killControllers:(NSArray<NSString *> *)killControllers {
NSMutableArray *controllers = self.viewControllers.mutableCopy;
@weakify(controllers);
[controllers enumerateObjectsWithOptions:NSEnumerationConcurrent
usingBlock:^(UIViewController * _Nonnull controller, NSUInteger vcidx, BOOL * _Nonnull vcstop) {
@strongify(controllers);
[killControllers enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([controller isKindOfClass:NSClassFromString(obj)]) {
[controllers removeObjectAtIndex:vcidx];
}
}];
}];
self.viewControllers = controllers;
}
/// pop到指定的controller
/// @param popController 目标Controller
- (void)popToController:(NSString *)popController {
NSMutableArray *controllers = self.viewControllers.mutableCopy;
[controllers enumerateObjectsWithOptions:(NSEnumerationReverse)
usingBlock:^(UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:NSClassFromString(popController)]) {
*stop = YES;
[self popToViewController:obj animated:YES];
}
}];
}
网友评论