AppDelegateを取得する
iOS, Mac OS Xで, プロジェクトを作成すると,かならず AppDelegate.h, AppDelegate.m
Application Delegateと呼ばれるものです。Applicationのスタートフックでもあります。
main.mにはこうあります。
int main(int argc, char *argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } }
このApplication Delegateにインスタンスなどをおいておけば, ほかのどのクラスからも参照できたりします。(工夫はいりますが)
CoreData では, Context, ほかSingletonのインスタンスなんかを AppDelegateで定義したり, アプリケーションを立ち上げたときに1回だけする操作などを書いたりします。
ほかのクラスからAppDelegateを取得する
idとして取得します。iOSではUIApplicationを使います。
id applicationDelegate = [[UIApplication sharedApplication] delegate];
MacOSの場合, NSApplicationになります。
id applicationDelegate = [[NSApplication sharedApplication] delegate];
クラスメソッドを使ってカジュアルに利用する
+(AppDelegate*)appdelegate { return [[UIApplication sharedApplication] delegate]; }
これを実装しておけばほかのクラスで
[AppleDelegate appdeleagte];
で取得できます。