您现在的位置是:首页 > 电脑 > 

我如何在旧版本的OS X上使用[SAlert beginSheetModalForWindow:completionhandler:](How can I use [SAlert beginSheetModalForWindow:completionhandler:] on older versi of OS X)

2025-07-28 15:59:30
我如何在旧版本的OS X上使用[SAlert beginSheetModalForWindow:completionhandler:](How can I use [SAlert beginSheetModalForWindow:completionhandler:] on older versi of OS X) OS X Mavericks实现了一
我如何在旧版本的OS X上使用[SAlert beginSheetModalForWindow:completionhandler:](How can I use [SAlert beginSheetModalForWindow:completionhandler:] on older versi of OS X)

OS X Mavericks实现了一个新的API,以更方便地显示SAlert :

- (void)beginSheetModalForWindow:(SWindow *)sheetWindow completionHandler:(void (^)(SModalRespe returnCode))handler

是否有一种简单的方法可以在OS X 10.8及更早版本中支持的类别中创建类似的方法?

OS X Mavericks implemented a new API for more convenient displaying of SAlert:

- (void)beginSheetModalForWindow:(SWindow *)sheetWindow completionHandler:(void (^)(SModalRespe returnCode))handler

Is there an easy way to create a similar method in a category that does the same thing but supported on OS X 10.8 and earlier?

最满意答案

是的,您可以使用基于委托的API来模拟类似的API。 唯一棘手的部分是让所有演员合适,因此它适用于ARC。 以下是SAlert的一个类别,它提供了一个向后兼容的基于块的API:

SAlert + BlockMethods.h

#import <Cocoa/Cocoa.h> @interface SAlert (BlockMethods) -(void)compatibleBeginSheetModalForWindow: (SWindow *)sheetWindow completionHandler: (void (^)(SInteger returnCode))handler; @end

SAlert +

#import "SAlert+BlockMethods.h" @implementation SAlert (BlockMethods) -(void)compatibleBeginSheetModalForWindow: (SWindow *)sheetWindow completionHandler: (void (^)(SInteger returnCode))handler { [self beginSheetModalForWindow: sheetWindow modalDelegate: self didEndSelector: @selector(blockBasedAlertDidEnd:returnCode:contextInfo:) contextInfo: (__bridge_retained void*)[handler copy] ]; } -(void)blockBasedAlertDidEnd: (SAlert *)alert returnCode: (SInteger)returnCode contextInfo: (void *)contextInfo { void(^handler)(SInteger) = (__bridge_transfer void(^)(SInteger)) contextInfo; if (handler) handler(returnCode); } @end

欲了解更多信息,请参阅我的SAlertBlockMethods Github回购 。

Yes, you can simulate a similar API using the delegate based API. The only tricky part is getting all the casts right so it works with ARC. Here's a category on SAlert that provides a backward compatible block-based API:

SAlert+BlockMethods.h

#import <Cocoa/Cocoa.h> @interface SAlert (BlockMethods) -(void)compatibleBeginSheetModalForWindow: (SWindow *)sheetWindow completionHandler: (void (^)(SInteger returnCode))handler; @end

SAlert+

#import "SAlert+BlockMethods.h" @implementation SAlert (BlockMethods) -(void)compatibleBeginSheetModalForWindow: (SWindow *)sheetWindow completionHandler: (void (^)(SInteger returnCode))handler { [self beginSheetModalForWindow: sheetWindow modalDelegate: self didEndSelector: @selector(blockBasedAlertDidEnd:returnCode:contextInfo:) contextInfo: (__bridge_retained void*)[handler copy] ]; } -(void)blockBasedAlertDidEnd: (SAlert *)alert returnCode: (SInteger)returnCode contextInfo: (void *)contextInfo { void(^handler)(SInteger) = (__bridge_transfer void(^)(SInteger)) contextInfo; if (handler) handler(returnCode); } @end

For more info, see my SAlertBlockMethods Github repo.

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/697398.html

相关标签:无
上传时间: 2023-08-27 09:54:27
留言与评论(共有 11 条评论)
本站网友 眼神光
12分钟前 发表
) contextInfo
本站网友 贺欣浩
11分钟前 发表
因此它适用于ARC
本站网友 斑丘疹
9分钟前 发表
see my SAlertBlockMethods Github repo.
本站网友 刚出生的小鱼吃什么
0秒前 发表
(void *)contextInfo { void(^handler)(SInteger) = (__bridge_transfer void(^)(SInteger)) contextInfo; if (handler) handler(returnCode); } @end For more info
本站网友 济宁影院
10分钟前 发表
self didEndSelector
本站网友 肝癌医院
13分钟前 发表
因此它适用于ARC
本站网友 亲人爱人
0秒前 发表
see my SAlertBlockMethods Github repo.
本站网友 卢龙房屋出租
4分钟前 发表
(__bridge_retained void*)[handler copy] ]; } -(void)blockBasedAlertDidEnd
本站网友 芦台农场
18分钟前 发表
请参阅我的SAlertBlockMethods Github回购
本站网友 info域名
29分钟前 发表
@selector(blockBasedAlertDidEnd