beginSheet:阻止替代方案?

发布于 2024-10-14 22:20:34 字数 77 浏览 1 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人接听 2024-10-21 22:20:35

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

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文