2
Snow Leopard是否引入了一些替代舊的beginSheet:方法,允許使用塊來完成整理工作?我不喜歡在另一個回調方法中使用它。beginSheet:block alternative?
Snow Leopard是否引入了一些替代舊的beginSheet:方法,允許使用塊來完成整理工作?我不喜歡在另一個回調方法中使用它。beginSheet:block alternative?
沒關係。我發現我正在尋找在這兩個網站:
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