ios 怎么知道当前显示的viewcontroller
遍历当前app的所有windows,寻找到等级为UIWindowLevelNormal的window
拿到该window的根控制器,循环遍历这个根控制器的presentedViewController直到找到当前显示的viewcontroller
参考的代码如下:
NSArray *windows = [UIApplication sharedApplication].windows;
UIWindow *keyWindow;
for (UIWindow *window in windows) {
if (window.windowLevel == UIWindowLevelNormal) {
keyWindow = window;
break;
}
}
UIViewController *topRootController = keyWindow.rootViewController;
while (topRootController.presentedViewController) {
topRootController = topRootController.presentedViewController;
}
最后的topRootController即为当前显示的视图控制器