beginSheet:阻止替代?

时间:2011-01-28 14:50:43

标签: objective-c cocoa cocoa-sheet

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

1 个答案:

答案 0 :(得分:5)

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

http://www.mikeash.com/pyblog/friday-qa-2009-08-14-practical-blocks.htmlhttp://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