项目里如何访问AppDelegate

项目里面访问AppDelegate做全局变量用有好几种方式

最原始就是

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

然后 appDelegate.yourMethord,但每个文件里面这么搞几下挺烦的,所以要想办法简略一下

1,有用宏的

#define AppDelegate (YourAppDelegate *)[[UIApplication sharedApplication] delegate]

2,扩展Catogory来访问的

有在UIApplication上扩展,也有NSObject上直接扩展,我觉得后面这个会影响一点效率吧

3,我选下面这种,直接在AppDelegate上暴露一个Class Methord 了事。

(1)static AppDelegate *appDelegate = nil;

@implementation AppDelegate

(2)

- ( BOOL)application:( UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary*)launchOptions {

appDelegate = self;

//......

}

(3)

+ (AppDelegate *)delegate{

return appDelegate;

}

其他上面提到的方式实现可见 http://*.com/questions/4141058/short-hand-for-uiapplication-sharedapplication-delegate 这里的讨论

上一篇:java.lang.String 类的所有方法


下一篇:JAVA中String类的intern()方法的作用