1. 通过UIApplication获取1
2
3UIWindow *window = [UIApplication sharedApplication].keyWindow;
或者
UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
2. 比较严谨的获取方法:1
2
3
4
5
6
7
8
9- (UIWindow *)lastWindow{
NSArray *windows = [UIApplication sharedApplication].windows;
for (UIWindow *window in [windows reverseObjectEnumerator]) {
if ([window isKindOfClass:[UIWindow class]] && CGRectEqualToRect(window.bounds, [UIScreen mainScreen].bounds)) {
return window;
}
}
return [UIApplication sharedApplication].keyWindow;
}
3. 查找响应者1
2
3
4
5
6
7
8
9
10
11// 新建一个UIView的类目,把这个方法放进去,以后就可以直接通过view.findResponderViewController来获取视图的控制器了。
- (UIViewController *)findResponderViewController{
UIResponder *next = self.nextResponder;
do {
if ([next isKindOfClass:[UIViewController class]]) {
return (UIViewController *)next;
}
next = next.nextResponder;
} while (next != nil);
return nil;
}
iOS 获取屏幕最上层window以及响应者
-------------本文结束感谢您的阅读-------------