IOS how to get the top level viewcontroller

Time:2021-6-12

1 get the viewcontroller of the current screen

//Gets the viewcontroller currently displayed on the screen
- (UIViewController *)getCurrentVC
{
 ///An analysis is given below
 UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
 UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
 return currentVC;
}

- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
 UIViewController *currentVC;
 if ([rootVC presentedViewController]) {
  //The view is presented
  rootVC = [rootVC presentedViewController];
 }

 if ([rootVC isKindOfClass:[UITabBarController class]]) {
  //The root view is uitab barcontroller
  currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]]; 
 } else if ([rootVC isKindOfClass:[UINavigationController class]]){
  //The root view is UINavigationController
  currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]]; 
 } else {
  //The root view is non navigational 
  currentVC = rootVC;
 }
 
 return currentVC;
}

2 Analysis

2.1 analysis of uiapplication

The core function of uiapplication is to provide control and cooperation during the running of IOS programs. Each program must have and only have one instance of uiapplication (or its subclass) during the running period. When the program starts and runs, a single instance of uiapplication will be created in the main function, In the code, you can get the pointer of this singleton instance by calling [uiapplication sharedapplication].

two point two   KeyWindow   A brief analysis of the development of the Internet

Before analyzing keywindow, let’s take a look at the concept of UIWindow

UIWindow is a subclass of uiview. It adds some view levels, manages views, forwards the properties and methods of uievent objects, and so on

In the above example, we use   [ Uiapplication shared application] to get the single instance object of uiapplication, and then get the currently active window (or the currently displayed main window) through the keywindow of the instance object

Keywindow refers to the active window in IOS development, that is, a window that can receive keyboard and non touch events. There can only be one keywindow at a time. In IOS development, we can set the front-end window by setting the value of uiwindowlevel. The higher the level is, the more forward the window will be. If the level of two windows is the same, Then we can display keywindow by making keyandvisible

(void)makeKeyWindow;// Make the current UIWindow a keywindow
(void)makeKeyAndVisible;// Change the current UI window into a keywindow and display it
[uiapplication sharedapplication]. Windows / / get all uiwindows of the current application
[uiapplication sharedapplication]. Keywindow / / gets the main window of the current application
View. Window / / get the UIWindow where a uiview is located

Makekeyandvisible and makekeywindow

  • Makekeywindow: only one thing has been done to make the current window the main window. The current window may not be displayed
  • Makekeyandvisible: do two things to make the current window the main window and display the current window. If we only want to display, we can also set its attribute hidden to No=

Becomekeywindow and resignkeywindow

  • Becomekeywindow: automatically called by the program to inform other windows that the current container has been set as the main window. We should not call this method actively. This method is automatically called by the system to send a notification. The default implementation of this method does not perform any operation, but the subclass can override it and use it to perform tasks related to becoming a key window.
  • Resignkeywindow: similar to becomekeywindow, it is called to inform the window that it is about to cancel the identity of the primary key window. Similarly, do not call this method directly.

2.3 properties of rootviewcontroller

As the name suggests: the root view of the current window

At present, only UIWindow has the property of rootviewcontroller, which should not be confused with the concept of root view in UINavigationController.
UINavigationController does not have the property of rootviewcontroller! There is no built-in setter method. To set its root view, you can only use the following methods


- (instancetype)initWithRootViewController:(UIViewController *)rootViewController; 

Get the root view of UIWindow

Mode one


AppDelegate *app =(AppDelegate *) [UIApplication sharedApplication].delegate;
UIViewController *rootViewController1 = appdelegate.window.rootViewController;

Mode 2


UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIViewController *rootViewController2 = window.rootViewController;

It should be noted that:

In mode 2, after uialertcontroller, UIAlertView and uiactionsheet pop up, the above views will appear, and a new window will be generated and added to the interface, so keywindow will become the class uialertcontrollershipresenterwindow

Brief analysis of 2.4 presented view controller

In IOS development, navigation controller is the general composition of the page   Or other uiviewcontroller, uitab viewcontroller, etc,

  • If there is navigation controller navigation bar, use [self. Navigationcoller pushview C] ontroller:animated :] to go to the next view and use [self. Navigationcontroller popviewcontrolleranimated:] to return to the previous view.
  • When there is no navigation controller navigation bar, use [self presentview C] ontroller:animated : completion:] to go to the next view, use [self disassviewcontroller a] nimated:completion :]; Return to the previous view.

Presentdviewcontroller and   presentingViewController

The case shows that A. presented view controller a jumps to B controller; B. Presentingviewcontroller is to return to controller a.

summary

Here is the article about how to get the top-level viewcontroller for IOS. For more information about how to get the top-level viewcontroller for IOS, please search previous articles of developer or continue to browse the following articles. I hope you can support developer more in the future!

Recommended Today

What is “hybrid cloud”?

In this paper, we define the concept of “hybrid cloud”, explain four different cloud deployment models of hybrid cloud, and deeply analyze the industrial trend of hybrid cloud through a series of data and charts. 01 introduction Hybrid cloud is a computing environment that integrates multiple platforms and data centers. Generally speaking, hybrid cloud is […]