[iOS]APP代码实践:建立一个辅助的APP类,减少对AppDelegate的修改
2016-02-13 16:12
666 查看
最开始接触iOS开发的时候,如果需要一些全局变量或者全局函数的时候,总是直接在AppDelegate中添加,因为AppDelegate可以直接获取
?
但是时间长了还是觉得这样不太好,AppDelegate本身有其自己的作用(对于App本身的一些事件进行处理,如启动,切换,推送),这样做感觉怪怪的,所以还是自己弄一个专门处理我们所需的全局变亮或者全局函数的对象会更好一些
?
然后 在预编译头*.pch中加入
?
就可以直接在代码的任意一个地方直接使用此类了,如
?
?
1 | [UIApplication sharedApplication].delegate |
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | //APPHelper.h @interface APPHelper + (APPHelper*)call; - (void) configureWindow:(UIWindow*)window; @property (nonatomic, readonly) AppDelegate *delegate; @property (strong, readonly) UIWindow *window; @end //APPHelper.m @interface APPHelper () @end @implementation APPHelper - (id)init { self = [super init]; if (self) { _delegate = (GGAppDelegate*)[UIApplication sharedApplication].delegate; } return self; } + (APPHelper *)call { static dispatch_once_t onceQueue; static APPHelper *appInstance; dispatch_once(&onceQueue, ^{ appInstance = [[APPHelper alloc] init]; }); return appInstance; } - (UIWindow *)window { return self.delegate.window; } - (void)configureWindow:(UIWindow*)window { UINavigationController *nav = [[UINavigationController alloc] init]; ... ... ... window.rootViewController = nav; } @end |
然后 在预编译头*.pch中加入
?
1 2 3 4 | #import "AppHelper.h" #define APP ([APPHelper call]) |
就可以直接在代码的任意一个地方直接使用此类了,如
?
1 2 3 | //设置APP为圆角 APP.window.layer.cornerRadius = 5.0f; APP.window.layer.masksToBounds = YES; |
相关文章推荐
- IOS 事件传递说明
- 本地化处理(IOS)
- iOS-基础动画
- IOS不用AutoLayout也能实现自动布局的类(5)----MyFlowLayout横空出世
- 在ios中使用soundtouch库实现变声
- ios 泛型
- IOS开发--待研究源码(持续添加更新)
- 01环信iOS SDK的集成
- Growth iOS 1.0版 发布——社区功能:更好的学习气氛
- iOS-AutoLayout布局学习
- iOS模拟器模拟器快捷键
- iOS设计模式FWZ
- ios轻量级开发FWZ
- iOS之小功能模块--彩虹动画进度条学习和自主封装改进
- iOS之NSDate
- iOS之数据解析 (JSON 、XML)
- iOS之延时器(NSTimer)
- iOS之运行时(Runtime)
- iOS 获取当前时间以及计算年龄(时间差)
- iOS "The sandbox is not in sync with the Podfile.lock"解决方式