美文网首页
ios获取当前界面,并判断当前界面是否为某个界面

ios获取当前界面,并判断当前界面是否为某个界面

作者: wenju | 来源:发表于2020-04-29 11:32 被阅读0次

    1.获取当前界面
    -(UIViewController) currentViewController {
    UIViewController
    viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    return [self atPersentViewController:viewController];
    }

    -(UIViewController)atPersentViewController:(UIViewController)vc {
    if (vc.presentedViewController) {
    return [self atPersentViewController:vc.presentedViewController];
    } else if ([vc isKindOfClass:[UISplitViewController class]]) {
    UISplitViewController* svc = (UISplitViewController) vc;
    if (svc.viewControllers.count > 0)
    return [self atPersentViewController:svc.viewControllers.lastObject];
    else
    return vc;
    } else if ([vc isKindOfClass:[UINavigationController class]]) {
    UINavigationController
    svc = (UINavigationController) vc;
    if (svc.viewControllers.count > 0)
    return [self atPersentViewController:svc.topViewController];
    else
    return vc;
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
    UITabBarController
    svc = (UITabBarController*) vc;
    if (svc.viewControllers.count > 0)
    return [self atPersentViewController:svc.selectedViewController];
    else
    return vc;
    } else {
    return vc;
    }
    }

    2.判断当前界面是否为某个界面
    //获取当前界面
    UIViewController *vcc = [self currentViewController];
    if ([vcc isKindOfClass:[某个界面名 class]]) {
    SSLog(@"vcc = %@",vcc.class);
    }

    相关文章

      网友评论

          本文标题:ios获取当前界面,并判断当前界面是否为某个界面

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