Last active
December 14, 2015 21:08
-
-
Save monjer/5148527 to your computer and use it in GitHub Desktop.
获取全局对象分类
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
添加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
使用NSObject+GetGlobalObject来方便的在自定义类(包括Controller,View,Model)中获取IOS常用的全局对象。