[IOS] APP code practice: Create an auxiliary APP class to reduce modifications to AppDelegate

Source: Internet
Author: User

[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 functionsAppDelegateBecauseAppDelegateCan be obtained directly

[UIApplication sharedApplication].delegate

However, after a long time, I still think this is not good,AppDelegateIt 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*.pchJoin

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.