beginSheet: block alternative?

邮差的信 提交于 2020-01-02 09:57:22

问题


Didn't Snow Leopard introduce some alternative to the old beginSheet: method that allows using a block to do the finishing stuff? I don't like having it in another callback method.


回答1:


Never mind. I found what I'm looking for at these two sites:

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

In fact, this is the code, and it's fully compatible with both GC and non-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


来源:https://stackoverflow.com/questions/4829529/beginsheet-block-alternative

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!