2

Snow Leopard 不是为旧的 beginSheet: 引入了一些替代方法,允许使用块来完成整理工作吗?我不喜欢在另一个回调方法中使用它。

4

1 回答 1

5

没关系。我在这两个网站上找到了我要找的东西:

http://www.mikeash.com/pyblog/friday-qa-2009-08-14-practical-blocks.html,http://www.cocoabuilder.com/archive/cocoa/281058-sheets-blocks-and- _ _垃圾收集器.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
于 2011-01-28T15:15:53.657 回答