NSAlert beginSheetModalForWindow:completionHandler:

Mik*_*r67 3 cocoa

NSAlert在Xcode 5.0.2中设置一个要显示为模态表的对象时,我发现了一个有趣的惊喜.

我正计划使用 beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo:

当我开始输入它时,Xcode beginSheetModalForWindow:completionHandler:为我自动填充(即使我在任何NSAlert文档中都找不到这个).

我更喜欢使用完成处理程序而不是委托/选择器作为回调机制,所以我继续尝试它.我惊喜地发现它完美无缺.

在我承诺之前有三个简单的问题.

  1. 我在文档中遗漏了什么吗?

  2. 如果没有文档,使用此功能是否"安全"? (也就是它会像神秘的一样神奇地消失?)

  3. 我宁愿不根据我通过日志记录看到的内容来硬编码响应值.有人知道"适当的" NS...Button常数吗?

Wil*_*ley 8

这个电话是"安全的",但它只有10.9+.这是来自头文件:

#if NS_BLOCKS_AVAILABLE
- (void)beginSheetModalForWindow:(NSWindow *)sheetWindow completionHandler:(void (^)(NSModalResponse returnCode))handler NS_AVAILABLE_MAC(10_9);
#endif
Run Code Online (Sandbox Code Playgroud)

看起来他们只是偶然地把它从当前的文档中删除了.标题通常被认为是Cocoa中的"真相",但它们权威地告诉你什么被弃用以及什么是新的.(例如,与X11不同,声明文档在实际实现或标题上是正确的.)

这些是您要在completionHandler块中使用的常量:

/* These are additional NSModalResponse values used by NSAlert's -runModal and -beginSheetModalForWindow:completionHandler:.

   By default, NSAlert return values are position dependent, with this mapping:
       first (rightmost) button = NSAlertFirstButtonReturn
       second button = NSAlertSecondButtonReturn
       third button = NSAlertThirdButtonReturn
       buttonPosition 3+x = NSAlertThirdButtonReturn + x

   Note that these return values do not apply to an NSAlert created via +alertWithMessageText:defaultButton:alternateButton:otherButton:informativeTextWithFormat:, which instead uses the same return values as NSRunAlertPanel.  See NSAlertDefaultReturn, etc. in NSPanel.h
*/
enum {
    NSAlertFirstButtonReturn    = 1000,
    NSAlertSecondButtonReturn   = 1001,
    NSAlertThirdButtonReturn    = 1002
};
Run Code Online (Sandbox Code Playgroud)