アラートを作成している私のメソッドはすぐに戻ります
@Joshが言うように、シートはそれが接続されているウィンドウに対してのみモーダルで実行されているためだと思います。アプリ全体がフリーズするわけではありません。したがって、実行するとすぐに、ユーザーがアラートに応答するのを待たずにbeginSheetModal...
、メソッドの残りの部分が実行され続け、(ここでは -1 を返します) で終了します。return returnCode
リターン コードは、ユーザーが最終的にプッシュするアラート パネルのボタンの代用です (NSAlertFirstButtonReturn、NSAlertSecondButtonReturn など - これらは NSAlert クラス ref の最後にリストされています)。メソッドでこれを使用してalertDidEnd
、ユーザーがアラートを閉じるために押したボタンに応じて動作します。そのため、alertDidEnd
セレクターには returnCode が含まれています。
一方、ブロック内でrunModal
メソッドを使用する場合は、メソッドを明示的に呼び出して、メソッドが終了したときに返される数値を渡す必要があります。これは、ユーザーがアラートを閉じたときです。else
alertDidEnd
runModal
コードの改訂版は次のとおりです。
int returnCode = -1;
if (displayAsSheet) {
[alert beginSheetModalForWindow:nativeWindow modalDelegate:delegate didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil];
// The selector alertDidEnd has the returnCode int. The alert will then set that code to whatever the user chooses, and will send the altered int on to alertDidEnd.
}
else {
// Here, everything stops once runModal is called, until the user dismisses the alert, at which time the runModal method returns the int representing the button the user pushed, and you assign the return to your variable "returnCode."
returnCode = [alert runModal];
[self alertDidEnd:alert returnCode:returnCode contextInfo:nil];
}
// Omit the line returning the returnCode.
次に、alertDidEnd
メソッドは次のようなことを行います。
- (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
switch (returnCode) {
case NSAlertFirstButtonReturn:
// Do whatever should happen when first button is pushed.
break;
case NSAlertSecondButtonReturn:
// Do whatever should happen when second button is pushed.
break;
default:
break;
}
// Unfreeze things.
[[NSApplication sharedApplication] stopModal];
}
ちなみに、必要に応じて、シートを実行して、シートが添付されているウィンドウだけでなく、アプリ全体をフリーズする方法があります:モーダル ヒント