How does iOS get the method of the controller in which View is currently located

  • 2020-12-05 17:23:24
  • OfStack

preface

I don't know if you've ever been in a wheel play, where you've got a little bit of a wheel play showing ads, some activities, etc., etc., etc., and when you click on a wheel play, you're going to jump to a different controller, and the click event is written in the controller, in order to avoid over-coding the controller and overloading the display. I've created a taxonomy for UIWindow, I'll call it Model (GetCurrentVC)

Implementation method

Google has many other methods. The following method is effective for pro measurement, and can be referred for reference if necessary.

1:


@interfaceUIWindow (GetCurrentVC)

- (UIViewController*)getCurrentVC;

@end

2:


#import"UIWindow+GetCurrentVC.h"

@implementationUIWindow (GetCurrentVC)

- (UIViewController*)getCurrentVC {

UIViewController*result =nil;

UIWindow* window = [[UIApplicationsharedApplication]keyWindow];

if(window.windowLevel!=UIWindowLevelNormal)

{

NSArray*windows = [[UIApplicationsharedApplication]windows];

for(UIWindow* tmpWininwindows)

{

if(tmpWin.windowLevel==UIWindowLevelNormal)

{

window = tmpWin;

break;

}

}

}

UIView*frontView = [[windowsubviews]objectAtIndex:0];

idnextResponder = [frontViewnextResponder];

if([nextResponderisKindOfClass:[UIViewControllerclass]])

result = nextResponder;

else

result = window.rootViewController;

returnresult;

}

@end

conclusion

The above is how iOS gets the current View controller implementation method, I hope this article can be helpful for you to develop iOS, if you have any questions, you can leave a message to communicate.


Related articles: