ブロックを使用して NSAlert を実行しようとしています。(最後に追加したボタンをキャンセル ボタンにしたい... 以下を参照) 自分のコードでそれを呼び出そうとすると、認識されないセレクターがクラスに送信されているというエラーが表示されます。拡張機能を不適切にセットアップしましたか?
ここに私の電話があります:
[NSAlert showSheetModalForWindow:self.window
WithTitle:@"Allow Sync"
message:message
informativeText:@"Data entered into Easy Spend Log will be shared."
alertStyle:NSWarningAlertStyle
cancelButtonTitle:@"Don't Allow"
otherButtonTitles:@[@"Allow",@"Always Allow"]
onDismiss:^(int buttonIndex) {
if (buttonIndex == 0)
[self.syncEngine allowSync:YES alwaysAllow:NO forConnection:connection];
else
[self.syncEngine allowSync:YES alwaysAllow:YES forConnection:connection];
}
onCancel:^ {
[self.syncEngine allowSync:NO alwaysAllow:NO forConnection:connection];
}];
NSAlert+Blocks.h
#import <Foundation/Foundation.h>
typedef void (^DismissBlock)(int buttonIndex);
typedef void (^CancelBlock)();
@interface NSAlert (Blocks)
+ (NSAlert*) showSheetModalForWindow:(NSWindow*) window
WithTitle:(NSString*) title
message:(NSString*) message
informativeText:(NSString*) text
alertStyle:(NSAlertStyle) style
cancelButtonTitle:(NSString*) cancelButtonTitle
otherButtonTitles:(NSArray*) otherButtons
onDismiss:(DismissBlock) dismissed
onCancel:(CancelBlock) cancelled;
@end
NSAlert+Blocks.m
#import "NSAlert+Blocks.h"
static DismissBlock _dismissBlock;
static CancelBlock _cancelBlock;
@implementation NSAlert (Blocks)
+ (NSAlert*) showSheetModalForWindow:(NSWindow*) window
WithTitle:(NSString*) title
message:(NSString*) message
informativeText:(NSString*) text
alertStyle:(NSAlertStyle) style
cancelButtonTitle:(NSString*) cancelButtonTitle
otherButtonTitles:(NSArray*) otherButtons
onDismiss:(DismissBlock) dismissed
onCancel:(CancelBlock) cancelled {
_cancelBlock = [cancelled copy];
_dismissBlock = [dismissed copy];
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:message];
[alert setInformativeText:text];
[alert setAlertStyle:style];
for(NSString *buttonTitle in otherButtons)
[alert addButtonWithTitle:buttonTitle];
[alert addButtonWithTitle:cancelButtonTitle];
[alert beginSheetModalForWindow:window modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)];
return alert;
}
+ (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
if(returnCode == [alert [buttons count]-1])
{
_cancelBlock();
}
else
{
_dismissBlock(returnCode - 1); // cancel button is button 0
}
}
@end