美文网首页
Get Currernt UIViewController Na

Get Currernt UIViewController Na

作者: 会飞的大西瓜v | 来源:发表于2020-03-03 11:57 被阅读0次
- (NSString *)getCurrentViewControllerName{

    dispatch_async(dispatch_get_main_queue(), ^{


    });
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    //获取根控制器
    UIViewController* currentViewController = window.rootViewController;
    //获取当前页面控制器
    BOOL runLoopFind = YES;
    while (runLoopFind){
     
        if (currentViewController.presentedViewController) {
            currentViewController = currentViewController.presentedViewController;
     
        } else if ([currentViewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController* navigationController = (UINavigationController* )currentViewController;
            currentViewController = [navigationController.childViewControllers lastObject];
     
        } else if ([currentViewController isKindOfClass:[UITabBarController class]]){
            UITabBarController* tabBarController = (UITabBarController* )currentViewController;
            currentViewController = tabBarController.selectedViewController;
        } else {
            NSUInteger childViewControllerCount = currentViewController.childViewControllers.count;
     
            if (childViewControllerCount > 0) {
                currentViewController = currentViewController.childViewControllers.lastObject;
                
            } else {
                return NSStringFromClass(currentViewController.class);;
            }
        }
    
    }
    return NSStringFromClass(currentViewController.class);;
}

相关文章

网友评论

      本文标题:Get Currernt UIViewController Na

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