Skip to content

Instantly share code, notes, and snippets.

@monjer
Last active December 14, 2015 21:08
Show Gist options
  • Save monjer/5148527 to your computer and use it in GitHub Desktop.
Save monjer/5148527 to your computer and use it in GitHub Desktop.
获取全局对象分类
#import <Foundation/Foundation.h>
@interface NSObject (GetGlobalObject)
@property (nonatomic , weak) UIScreen *mainScreen ;
@property (nonatomic , weak) UIApplication *app ;
@property (nonatomic , weak) UIWindow *keyWindow ;
@property (nonatomic , weak) UIWindow *contentWindow ;
@property (nonatomic , weak) UIViewController *rootVC ;
@property (nonatomic, assign) id<UIApplicationDelegate> appDelegate ;
@property (nonatomic,weak) NSNotificationCenter *msgCenter ;
@end
#import "NSObject+GetGlobalObject.h"
@implementation NSObject (GetGlobalObject)
@dynamic mainScreen , app , keyWindow , contentWindow , rootVC , appDelegate , msgCenter;
- (UIScreen*)mainScreen
{
return [UIScreen mainScreen];
}
- (UIApplication*)app
{
return [UIApplication sharedApplication] ;
}
- (UIWindow *)keyWindow
{
return self.app.keyWindow ;
}
- (UIWindow *)contentWindow
{
return self.app.delegate.window ;
}
- (UIViewController*)rootVC
{
return self.contentWindow.rootViewController ;
}
- (id<UIApplicationDelegate>)appDelegate
{
return self.app.delegate ;
}
- (NSNotificationCenter *)msgCenter
{
return [NSNotificationCenter defaultCenter];
}
@end
@monjer
Copy link
Author

monjer commented Mar 25, 2013

添加contentWindow属性,修改keyWindow的bug。在弹出UIAlertView后,keyWindow已经不在是delegate的window,所以rootVC应该在contentWindow属性中获取

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment