我如何在旧版本的OS X上使用[NSAlert beginSheetModalForWindow:completionhandler:](How can I use [NSAlert beginShe

编程入门 行业动态 更新时间:2024-10-26 08:31:32
我如何在旧版本的OS X上使用[NSAlert beginSheetModalForWindow:completionhandler:](How can I use [NSAlert beginSheetModalForWindow:completionhandler:] on older versions of OS X)

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

- (void)beginSheetModalForWindow:(NSWindow *)sheetWindow completionHandler:(void (^)(NSModalResponse returnCode))handler

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

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

- (void)beginSheetModalForWindow:(NSWindow *)sheetWindow completionHandler:(void (^)(NSModalResponse 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。 以下是NSAlert的一个类别,它提供了一个向后兼容的基于块的API:

NSAlert + BlockMethods.h

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

NSAlert + BlockMethods.m

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

欲了解更多信息,请参阅我的NSAlertBlockMethods 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 NSAlert that provides a backward compatible block-based API:

NSAlert+BlockMethods.h

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

NSAlert+BlockMethods.m

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

For more info, see my NSAlertBlockMethods Github repo.

更多推荐

本文发布于:2023-08-06 20:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455021.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:旧版本   如何在   OS   NSAlert   versions

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!