1. Get the current screen Viewcontroller case source code 2. UIApplication saving-energy-and-saving-land 3. KeyWindow saving-energy-and-saving-land 4. RootViewController saving-energy-and-saving-land 5. PresentedViewController The analysis of

1 Get the Viewcontroller displayed on the current screen

// Get the viewController displayed on the current screen
- (UIViewController *)getCurrentVC
{
   /// The following is the analysis
    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 UITabBarController
        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 a non-navigation class
        currentVC = rootVC;
    }
    
    return currentVC;
}
Copy the code
2 analysis
2.1 Brief analysis of UIApplication

The core role of UIApplication is to provide control and collaboration during the runtime of iOS applications. Each application must have one and only one instance of UIApplication (or a subclass of IT) at runtime. A singleton instance of UIApplication is created in main, and you can get a pointer to this singleton in your code by calling [UIApplication sharedApplication].

2.2 Brief analysis of KeyWindow

Let’s take a look at the concept of UIWindow before we analyze KeyWindow

UIWindow is a subclass of UIView, and it adds some view hierarchy to UIView, managing views, forwarding properties and methods of UIEvent objects, and so on

In the example above, we get the singleton instance object of UIApplication using [UIApplication sharedApplication], The instance object’s keyWindow is then used to retrieve the currently active window(or the main window currently displayed).

KeyWindow is an active window in IOS development. It is a window that can receive keyboard and non-touch events. There can only be one KeyWindow at a time In development, we can set the value of UIWindowLevel to set which window is the most front window. The higher the value of Level, the higher the window. If the two Windows have the same Level, we can show KeyWindow through makeKeyAndVisible

(void)makeKeyWindow; // Make the current UIWindow keyWindow (main window)

(void)makeKeyAndVisible; // Make the current UIWindow keyWindow, [UIApplication sharedApplication].windows // Get all uiWindows [UIApplication sharedApplication].keyWindow for the current application // Get the main window of the current application view.window /// get the UIWindow where a UIView is located

MakeKeyAndVisible and makeKeyWindow

  • MakeKeyWindow: The only thing it does is make the current window the main window. The current window may not be displayed

  • MakeKeyAndVisible: Does two things to make the current window the main window and display the current window. If you want to display only, you can also set the property hidden to NO. =

BecomeKeyWindow and resignKeyWindow

  • BecomeKeyWindow: An automatic program call that notifying other Windows that the current container is set as the main window. We don’t want to call this method actively, this method is called automatically by the system for notifications, the default implementation of this method does nothing, but subclasses can override it and use it to perform tasks related to being a critical window.

  • ResignKeyWindow: Similar to becomeKeyWindow, which is called to notify the window that it is going to deactivate the primary key window. Again, do not call this method directly.

2.3 a rootViewController property

As the name implies: The root view of the current window currently only UIWindow has the rootViewController property, not to be confused with the root view concept in UINavigationController. UINavigationController does not have a rootViewController property! There’s no built-in setter. The only way to set its root view is by doing the following

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController; 
Copy the code

Gets the root view of uiWindow

Methods a

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

Way 2

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

Copy the code

Note that:

In method two, when UIAlertController, UIAlertView, UIActionSheet pops up, those views come up and create a new window that’s added to the interface, So keyWindow becomes UIAlertControllerShimPresenterWindow this class

2.4 PresentedViewController analysis

In ios development, a typical page consists of NavigationController or other UiViewController, UITabViewController, etc.

  • In have the NavigationController navigation, the use of [self. NavigationColler pushViewController: animated:] to the next view, Use [self navigationController popViewControllerAnimated:] to return to the view.

  • In the absence of NavigationController navigation, the use of [self presentViewController: animated: completion:] to the next view, Use [self dismissViewControllerAnimated: completion:]; Return to the previous view.

PresentedViewController and presentingViewController

Explains how Amy polumbo resentedViewController controller is A controller to jump to B; P. resentingViewController is returned to A controller.