applicationWillTerminate: のメモ
NSApplicationDelegate Protocol の applicationWillTerminate: についてメモ。
applicationWillTerminate: は、アプリケーションが終了する直前に呼ばれる。ドキュメントによる最後のクリーンアップ処理を行うことを想定している。
applicationWillTerminate: は、applicationShouldTerminate: が NSTerminateNow を返したときに通知される。
そして、ウィンドウがひとつしかない場合や最後のウィンドウが閉じた場合には applicationShouldTerminateAfterLastWindowClosed: が呼ばれ、YESを返した場合には上記の applicationShouldTerminate: が呼ばれる。
まとめてみる。
- (BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication *)theApplication {
// 最後のウィンドウが閉じられた
// 問題があればNOを返す。問題がなければYESを返す。
return YES; // applicationShouldTerminate: が呼ばれる
}
- (NSApplicationTerminateReply)applicationShouldTerminate: (NSApplication *)sender
{
// NSAppが終了しようとしている。
// 問題があれば NSTerminateCancel を返す。
// 問題がなければ NSTerminateNow を返す。
return NSTerminateNow; // applicationWillTerminate: が呼ばれる
}
- (void) applicationWillTerminate: (NSNotification *)aNotification
{
// 最後のクリーンアップ処理を行う。
}
NSApplication の terminate: は、メニューの [アプリケーションを終了 ⌘Q] を実行すると勝手に呼ばれる。
ちなみに、terminate: は applicationShouldTerminate: を実行する。
普通にアプリケーションを終了する場合には、terminate: は実行しなくていい。
そんな感じ。
カテゴリー: Cocoa, Mac, Objective-C