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