NSAlert + Blocks - 需要协助设置新的扩展

时间:2012-12-14 20:49:51

标签: objective-c macos objective-c-blocks objective-c-category

我正在尝试使用块来执行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

1 个答案:

答案 0 :(得分:1)

一旦我设置了测试项目,问题就变得明显了。在类别.m文件中调用警报是错误的。

我已将工作代码发布到github上。 https://github.com/AaronBratcher/NSAlert-Blocks