objective-c – 如何在旧版本的OS X上使用[NSAlert beginSheetModalForWindow:completionhandler:]

objective-c – 如何在旧版本的OS X上使用[NSAlert beginSheetModalForWindow:completionhandler:],第1张

概述OS X Mavericks实现了一个新的API,以便更方便地显示NSAlert: - (void)beginSheetModalForWindow:(NSWindow *)sheetWindow completionHandler:(void (^)(NSModalResponse returnCode))handler 有没有一种简单的方法可以在OS X 10.8及更早版本支持的类别中创建类似 OS X Mavericks实现了一个新的API,以便更方便地显示NSAlert:

- (voID)beginSheetModalForWindow:(NSWindow *)sheetwindow completionHandler:(voID (^)(NSModalResponse returnCode))handler

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

解决方法 是的,您可以使用基于委托的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 repo.

总结

以上是内存溢出为你收集整理的objective-c – 如何在旧版本的OS X上使用[NSAlert beginSheetModalForWindow:completionhandler:]全部内容,希望文章能够帮你解决objective-c – 如何在旧版本的OS X上使用[NSAlert beginSheetModalForWindow:completionhandler:]所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1006710.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存