您的位置:首页 > 移动开发 > IOS开发

[iOS]APP代码实践:建立一个辅助的APP类,减少对AppDelegate的修改

2015-12-15 10:32 495 查看

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载。如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^ 
我要捐赠: 点击捐赠Cocos2d-X源码下载:点我传送游戏官方下载:http://dwz.cn/RwTjl游戏视频预览:http://dwz.cn/RzHHd游戏开发博客:http://dwz.cn/RzJzI游戏源码传送http://dwz.cn/Nret1
最开始接触iOS开发的时候,如果需要一些全局变量或者全局函数的时候,总是直接在
AppDelegate
中添加,因为
AppDelegate
可以直接获取
1
[UIApplication sharedApplication].delegate
但是时间长了还是觉得这样不太好,
AppDelegate
本身有其自己的作用(对于App本身的一些事件进行处理,如启动,切换,推送),这样做感觉怪怪的,所以还是自己弄一个专门处理我们所需的全局变亮或者全局函数的对象会更好一些
12
3
4
5
6
7
8
9
10
1112
13
14
15
16
17
18
19
20
2122
23
24
25
26
27
28
29
30
3132
33
34
35
36
37
38
39
40
4142
43
44
45
46
47
48
49
50
5152
53
54
55
56
57
58
59
60
6162
63
64
65
66
67
68
69
//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
中加入
12
3
4
#import "AppHelper.h"

#define APP ([APPHelper call])
就可以直接在代码的任意一个地方直接使用此类了,如
12
3
4
//设置APP为圆角
APP.window.layer.cornerRadius = 5.0f;
APP.window.layer.masksToBounds = YES;

郝萌主倾心贡献,尊重作者的劳动成果,请勿转载。如果文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额随意,重在心意^_^ 
我要捐赠: 点击捐赠Cocos2d-X源码下载:点我传送游戏官方下载:http://dwz.cn/RwTjl游戏视频预览:http://dwz.cn/RzHHd游戏开发博客:http://dwz.cn/RzJzI游戏源码传送http://dwz.cn/Nret1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: