[IOS] APP code practice: Create an auxiliary APP class to reduce modifications to AppDelegate
When I first started iOS development, if I needed some global variables or global functionsAppDelegate
BecauseAppDelegate
Can be obtained directly
[UIApplication sharedApplication].delegate
However, after a long time, I still think this is not good,AppDelegate
It has its own role (to process some events of the App, such as start, switch, and push). This is strange, therefore, it is better to create an object that specifically handles global brightening or global functions we need.
//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
Then in the pre-compiled Header*.pch
Join
#import "AppHelper.h"#define APP ([APPHelper call])
This class can be directly used in any part of the Code, such
// Set APP to rounded corner APP. window. layer. cornerRadius = 5.0f; APP. window. layer. masksToBounds = YES;