小轮子:router

2017-12-21  本文已影响0人  dc630f46ee2d
#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
上一篇 下一篇

猜你喜欢

热点阅读