In the NSObject subclass, obtain the ViewController displayed on the current screen. The current viewcontroller

Source: Internet
Author: User

In the NSObject subclass, obtain the ViewController displayed on the current screen. The current viewcontroller

When we want to display a view at any time in a non-view class, we need to add the displayed view to the subview of the current view, or use the current view presentViewController or pushViewContrller, these operations require obtaining the currently displayed ViewController.

The Code is as follows: (read the notes carefully for detailed understanding)

# Pragma mark get the viewcontroller displayed on the current screen-(UIViewController *) getCurrentVC {// define a variable to store the viewcontroller UIViewController * result = nil displayed on the current screen; // obtain the main window UIWindow * window of the current application = [[UIApplication sharedApplication] keyWindow]; // windowLevel is the position of the window on the Z axis. The default value is UIWindowLevelNormal if (window. windowLevel! = UIWindowLevelNormal) {// obtain all windows of the application. NSArray * windows = [[UIApplication sharedApplication] windows]; for (UIWindow * tmpWin in windows) {// find the default window of the Program (displayed window) if (tmpWin. windowLevel = UIWindowLevelNormal) {// assign a key window to the default window = tmpWin; break ;}}} // obtain the current display view of the window. UIView * frontView = [[window subviews] objectAtIndex: 0]; // obtain the next response of the view, the returned value of this method is UIViewController or its parent view id nextResponder = [frontView nextResponder]. // determine whether the next responder of the display view is a class Object of UIViewController if ([nextResponder isKindOfClass: [UIViewController class]) {result = nextResponder;} else {result = window. rootViewController;} return result ;}

Code Description:

  • Description of the UIApplication class attributes used in the Code:

KeyWindow (@ property (nonatomic, readonly) UIWindow * keyWindow)

Official Website description:

The value of this property is YES when the window is the key window or NO when it is not. the key window names es keyboard and other non-touch related events. only one window at a time may be the key window.

Personal Understanding:

Main Window of the application

Windows (@ property (nonatomic, readonly) NSArray <__kindof UIWindow *> * windows)

Official Website description:

This property contains the UIWindow objects currently associated with the app. This list does not include windows created and managed by the system, such as the window used to display the status bar.

Personal Understanding:

Obtain all the windows displayed and hidden in the application and put them in an array.

  • Description of the UIWindow class attributes used in the Code:

WindowLevel (@ property (nonatomic) UIWindowLevel windowLevel)

Official Website description:

Window levels provide a relative grouping of windows along the z-axis. all windows assigned to the same window level appear in front of (or behind) all windows assigned to a different window level. the ordering of windows within a given window level is not guaranteed.

Personal Understanding:

The window position in the zaxis direction. The default value is UIWindowLevelNormal.

  • Descriptions of object methods in the UIResponder class used in the Code:

-(UIResponder *) nextResponder

Official Website description:

The UIResponder class does not store or set the next responder automatically, instead returning nil by default. subclasses must override this method to set the next responder. UIView implements this method by returning the UIViewController object that manages it (if it has one) or its superview (if it doesn' t ); UIViewController implements the method by returning its view's superview; UIWindow returns the application object, and UIApplication returns nil.

Personal Understanding:

Returns the next responder, that is, the next in the execution sequence.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.