項目裏面訪問AppDelegate作全局變量用有好幾種方式 app
最原始就是 ui
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; spa
而後 appDelegate.yourMethord,但每一個文件裏面這麼搞幾下挺煩的,因此要想辦法簡略一下 code
1,有用宏的 it
#define AppDelegate (YourAppDelegate *)[[UIApplication sharedApplication] delegate]2,擴展Catogory來訪問的
有在UIApplication上擴展,也有NSObject上直接擴展,我以爲後面這個會影響一點效率吧 io
3,我選下面這種,直接在AppDelegate上暴露一個Class Methord 了事。 class
(1)static AppDelegate *appDelegate = nil; 效率
@implementation AppDelegate 變量
(2) 擴展
- ( BOOL)application:( UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions {appDelegate = self;
//......
}
(3)
+ (AppDelegate *)delegate{
return appDelegate;
}
其餘上面提到的方式實現可見 http://stackoverflow.com/questions/4141058/short-hand-for-uiapplication-sharedapplication-delegate 這裏的討論