项目里面访问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 这里的讨论