2011-01-28 5 views
2

Snow Leopardでは、ブロックを使用して仕上げ作業を行うことができる古いbeginSheet:メソッドの代替手段はありませんでしたか?私は別のコールバックメソッドでそれを持つのが好きではありません。beginSheet:代替ブロック?

答えて

5

気にしないでください。私はこの二つのサイトで探しているものを見つける:

http://www.mikeash.com/pyblog/friday-qa-2009-08-14-practical-blocks.html、実際には http://www.cocoabuilder.com/archive/cocoa/281058-sheets-blocks-and-garbage-collector.html

、これはコードであり、それはGCと非GCの両方と完全に互換性があります:

@implementation NSApplication (SheetAdditions) 

- (void)beginSheet:(NSWindow *)sheet modalForWindow:(NSWindow *)docWindow didEndBlock:(void (^)(NSInteger returnCode))block 
{ 
    [self beginSheet:sheet 
    modalForWindow:docWindow 
    modalDelegate:self 
    didEndSelector:@selector(my_blockSheetDidEnd:returnCode:contextInfo:) 
     contextInfo:Block_copy(block)]; 
} 

- (void)my_blockSheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo 
{ 
    void (^block)(NSInteger returnCode) = contextInfo; 
    block(returnCode); 
    Block_release(block); 
} 

@end