美文网首页
小轮子:router

小轮子:router

作者: dc630f46ee2d | 来源:发表于2017-12-21 15:38 被阅读0次
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface DishRouteTarget : NSObject
@property (nonatomic, strong, readonly) UIViewController *targetViewController;
+ (DishRouteTarget *)routeTarget:(UIViewController *)viewController;
@end
#import "DishRouteTarget.h"
@interface DishRouteTarget ()
@property (nonatomic, strong, readwrite) UIViewController *targetViewController;
@end

@implementation DishRouteTarget
+ (DishRouteTarget *)routeTarget:(UIViewController *)viewController {
    return [[DishRouteTarget alloc] initWithController:viewController];
}

- (instancetype)initWithController:(UIViewController *)viewController {
    self = [super init];
    if (self) {
        _targetViewController = viewController;
    }
    return self;
    
}
@end
#import <UIKit/UIKit.h>
#import "DishRouteTarget.h"

@interface DishRouteManager : UIViewController
+ (void)routeTo:(DishRouteTarget *)target;
@end

@interface DishRouteManager (Convenient)
+ (void)routTo:(UIViewController *)viewController;
@end
#import "DishRouteManager.h"

@interface DishRouteManager ()

@end

@implementation DishRouteManager


+ (UIViewController *)topViewController {
    UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController;
    return [self topViewController:viewController];
}

+ (UIViewController *)topViewController:(UIViewController *)rootViewController {
    if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nav = (UINavigationController *)rootViewController;
       return [self topViewController:nav.viewControllers.topViewController];
    }
    else if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tab = (UITabBarController *)rootViewController;
        return [self topViewController:tab.selectedViewController];
    }
    else if (rootViewController.presentedViewController) {
       return [self topViewController:rootViewController.presentedViewController];
    }
    return rootViewController;
}

+ (void)routeTo:(DishRouteTarget *)target {
    if (target.targetViewController == nil) {
        return;
    }
    UIViewController *viewController = [DishRouteManager topViewController];
    if (viewController.navigationController) {
        UINavigationController *nav = viewController.navigationController;
        if ([nav.viewControllers containsObject:target.targetViewController]) {
            [nav popToViewController:target.targetViewController animated:YES];
        } else {
            [nav pushViewController:target.targetViewController animated:YES];
        }
    }
    else if (viewController.tabBarController) {
        UITabBarController *tab = viewController.tabBarController;
        if ([tab.viewControllers containsObject:target.targetViewController ]) {
            [tab setSelectedViewController:target.targetViewController ];
        }else {
            [tab addChildViewController:target.targetViewController];
            [tab setSelectedViewController:target.targetViewController];
        }
    }
}

@end

@implementation DishRouteManager (Convenient)
+ (void)routTo:(UIViewController *)viewController {
    DishRouteTarget *target = [DishRouteTarget routeTarget:viewController];
    [self routeTo:target];
}

@end

相关文章

  • 小轮子:router

  • 页面路由-Swift-ACRouter

    ACRouter ACRouter 造轮子的由来 现在已经有很成熟的Router工具, 如 JLRoutes 与 ...

  • 小轮子:loading

  • 常用小轮子

    english_punctuations = [',', '.', ':', ';', '?', '(', ')'...

  • 小谈造轮子

    最近一直在gitHub 论坛上看到这个词语,开始不明觉厉,闲下来的时间仔细去查找了一些来源。引用知乎上一位说的很好...

  • 车盲

    我是一个车盲。 对车的定义仅限于有几个轮子。 比如: 一个轮子是独轮车, 两个轮子是自行车或摩托车, 四个轮子是小...

  • 小电驴

    不知是什么原因,解二他儿子竟然自说自话买了小电驴,小牛牌。家里车子不少,两个轮子、三个轮子、四个轮子的都有,他自己...

  • 轮子,还是轮子

    从一个社会呃呃呃轮子应用程度可以看出一个城市的繁华程度。 大的轮子是飞机轮,汽车轮,地铁轮,高铁轮,小的轮子是外卖...

  • 2019-05-31 程序员修仙进阶标准,你到哪个阶段了?

    闭门造轮子 > 使用别人的轮子 > 开门造轮子 > 分享轮子

  • 午后斜阳追随谁

    时间的小轮子 在午后 在我饮毕一杯热茶然后 关闭窗子 驱动着我踱步楼下走在街上 小轮子无孔不入带着我 穿过墓地 ...

网友评论

      本文标题:小轮子:router

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