Jak*_*ger 4 cocoa objective-c objective-c-blocks
OS X Mavericks实现了一个新的API,以便更方便地显示NSAlert:
- (void)beginSheetModalForWindow:(NSWindow *)sheetWindow completionHandler:(void (^)(NSModalResponse returnCode))handler
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以在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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅我的NSAlertBlockMethods Github repo.
| 归档时间: |
|
| 查看次数: |
1610 次 |
| 最近记录: |