[iOS]APP代码实践:建立一个辅助的APP类,降低对AppDelegate的改动

郝萌主倾心贡献。尊重作者的劳动成果,请勿转载。

假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意,重在心意^_^ 

我要捐赠: 点击捐赠

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本身的一些事件进行处理,如启动,切换,推送),这样做感觉怪怪的,所以还是自己弄一个专门处理我们所需的全局变亮或者全局函数的对象会更好一些

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
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中加入

1
2
3
4
#import "AppHelper.h"


#define APP ([APPHelper call])

就能够直接在代码的任意一个地方直接使用此类了,如

1
2
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

原文地址:https://www.cnblogs.com/mthoutai/p/7210821.html