Skip to content

Instantly share code, notes, and snippets.

@beelsebob
Forked from PsychoH13/PSYBlockTimer.h
Created October 27, 2011 13:51
Show Gist options
  • Select an option

  • Save beelsebob/1319587 to your computer and use it in GitHub Desktop.

Select an option

Save beelsebob/1319587 to your computer and use it in GitHub Desktop.
Fire an NSTimer using a block as execution code.
//
// NSAlertWithBlocks.h
//
// Created by Thomas Davie on 26/12/2009.
// Copyright 2009 Thomas Davie. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface NSAlert (WithBlocks)
- (void)beginSheetModalForWindow:(NSWindow *)window completionHandler:(void(^)(NSInteger result))completionHandler;
@end
//
// NSAlertWithBlocks.m
//
// Created by Thomas Davie on 26/12/2009.
// Copyright 2009 Thomas Davie. All rights reserved.
//
#import "NSAlertWithBlocks.h"
@interface NSAlert (WithBlocksPrivate)
- (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;
@end
@implementation NSAlert (WithBlocks)
- (void)beginSheetModalForWindow:(NSWindow *)window completionHandler:(void(^)(NSInteger result))completionHandler
{
[self beginSheetModalForWindow:window modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:[completionHandler copy]];
}
- (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
void(^handler)(NSInteger result) = (void(^)(NSInteger result))contextInfo;
handler(returnCode);
[handler release];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment