在iOS 开发过程中,我们对页面进行操作的时候,经常会获取当前控制器controller或者视图view的window。下面给大家分享一个方法
/**
获取当前的window,不一定是keywindow
@return 当前window
*/
-(UIWindow*)mainWindow{
id appDelegate = [UIApplication sharedApplication].delegate;
if (appDelegate && [appDelegate respondsToSelector:@selector(window)]) {
return [appDelegate window];
}
NSArray *windows = [UIApplication sharedApplication].windows;
if ([windows count] == 1) {
return [windows firstObject];
} else {
for (UIWindow *window in windows) {
if (window.windowLevel == UIWindowLevelNormal) {
return window;
}
}
}
return nil;
}
搞定!