[iOS]APP代碼實踐:建立一個輔助的APP類,減少對AppDelegate的修改

來源:互聯網
上載者:User

[iOS]APP代碼實踐:建立一個輔助的APP類,減少對AppDelegate的修改

最開始接觸iOS開發的時候,如果需要一些全域變數或者全域函數的時候,總是直接在AppDelegate中添加,因為AppDelegate可以直接擷取

[UIApplication sharedApplication].delegate

但是時間長了還是覺得這樣不太好,AppDelegate本身有其自己的作用(對於App本身的一些事件進行處理,如啟動,切換,推送),這樣做感覺怪怪的,所以還是自己弄一個專門處理我們所需的全域變亮或者全域函數的對象會更好一些

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

#import "AppHelper.h"#define APP ([APPHelper call])

 

就可以直接在代碼的任意一個地方直接使用此類了,如

//設定APP為圓角APP.window.layer.cornerRadius = 5.0f;APP.window.layer.masksToBounds = YES;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.