Method for IOS to obtain the current viewcontroller

Time:2021-10-19

This article introduces IOS to obtain the current viewcontroller and share it with you. The details are as follows

By simply judging [uiviewcontroller class], it is wrong to determine that it is the desired controller:


if ([nextResponder isKindOfClass:[UIViewController class]]) {
    result = nextResponder;
}else {
    result = window.rootViewController;
}

Because: iskindofclass: determines whether an object is a member of a class or a member derived from that class.

According to the class diagram of IOS, there are several derived classes of uiviewcontroller class. What we need to distinguish is uitabbarcontroller and UINavigationController. We don’t want to get them.

1) Uitabbarcontroller holds multiple viewcontrollers through the attribute viewcontrollers;
2) The UINavigationController holds or removes the viewcontroller by pressing and exiting the stack;
3) There is no need to judge the uitableviewcontroller. It is a single viewcontroller, and it is more about creating tableviews by handwriting.

Provide a complete method:

+ (UIViewController *)getCurrentViewController {
  UIViewController *result = nil;
  UIWindow * window = [[UIApplication sharedApplication] keyWindow];
  //The default windowlevel of app is uiwindowlevelnormal. If not, find it
  if (window.windowLevel != UIWindowLevelNormal) {
    NSArray *windows = [[UIApplication sharedApplication] windows];
    for(UIWindow * tmpWin in windows) {
      if (tmpWin.windowLevel == UIWindowLevelNormal) {
        window = tmpWin;
        break;
      }
    }
  }
  id nextResponder = nil;
  UIViewController *appRootVC = window.rootViewController;
  //1. Pop up VC through present. Approotvc.presentedviewcontroller is not nil
  if (appRootVC.presentedViewController) {
    nextResponder = appRootVC.presentedViewController;
  }else{
    //2. Pop up VC through navigationcontroller
    NSLog(@"subviews == %@",[window subviews]);
    UIView *frontView = [[window subviews] objectAtIndex:0];
    nextResponder = [frontView nextResponder];
  }
  //1、tabBarController
  if ([nextResponder isKindOfClass:[UITabBarController class]]){
    UITabBarController * tabbar = (UITabBarController *)nextResponder;
    UINavigationController * nav = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex];
    //Or UINavigationController * NAV = tabbar.selectedviewcontroller;
    result = nav.childViewControllers.lastObject;
  }else if ([nextResponder isKindOfClass:[UINavigationController class]]){
    //2、navigationController
    UIViewController * nav = (UIViewController *)nextResponder;
    result = nav.childViewControllers.lastObject;
  }else{//3、viewControler
    result = nextResponder;
  }
  return result;
}

From the above code, you can derive the current tabbarcontroller and navigationcontroller, which may be used sometimes.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]