2014-01-11 42 views
3

當設置一個NSAlert對象作爲Xcode 5.0.2中的模式表單時,我感到一個有趣的驚喜。NSAlert beginSheetModalForWindow:completionHandler:

我打算用beginSheetModalForWindow:modalDelegate:didEndSelector:contextInfo:

當我開始進入它,Xcode的自動填充beginSheetModalForWindow:completionHandler:我(儘管我不能任何NSAlert文檔中找到)。

我更喜歡使用完成處理程序而不是委託/選擇器作爲回調機制,所以我繼續嘗試。我驚喜地發現它完美運作。

在我承諾之前提出三個簡單的問題。

  1. 我在文檔中丟失了什麼嗎?

  2. 如果使用此功能是無證的,是否「安全」? (即它會神奇地消失,因爲它出現?)

  3. 我寧願不根據我通過日誌記錄看到的響應值。有人知道「正確的」NS...Button常量嗎?

回答

8

此調用爲「安全」,但僅爲10.9+。這是頭文件:

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

看來,他們只是不小心留下它當前文檔的。然而,標題通常被認爲是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 
}; 
+0

我m建立在運行10.9的機器上,但指定了10.6作爲部署目標。這是否意味着當我最終在10.6機器上進行測試時,這會失敗? – MikeMayer67

+0

是啊,你必須使用10.6的舊方法,否則會崩潰。很愚蠢,當你使用「太新」的方法時,沒有任何警告,但這是他們的決定。 –