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

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

2016-02-13 16:12 666 查看
最开始接触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

//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;

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: