2010-01-10 121 views
1

我試圖編寫一些簡單的代碼,將文本文件的內容拖放到窗口上。在前面的帖子和蘋果例子的幫助下,我現在已經掌握了拖放和運行的基礎知識。拖放粘貼板數據

不幸的是,Apple的示例代碼只涉及圖像。任何人都可以告訴我如何修改他們的「粘貼板」方法(如下所示)來發送一個簡單的'dot.txt'文件的內容?

- (void)pasteboard:(NSPasteboard*)sender provideDataForType:(NSString*)type 
{ 
//------------------------------------------------------ 
// method called by pasteboard to support promised drag types. 
//-------------------------------------------------------- 
//sender has accepted the drag and now we need to send the data for the type we promised 
if([type compare: NSTIFFPboardType]==NSOrderedSame) 
    { 
    //set data for TIFF type on the pasteboard as requested 
    [sender setData:[[self image] TIFFRepresentation] forType:NSTIFFPboardType]; 
    } 
else if([type compare: NSPDFPboardType]==NSOrderedSame) 
    { 
    [sender setData:[self dataWithPDFInsideRect:[self bounds]] forType:NSPDFPboardType]; 
    } 
} 

感謝:-)

回答

1

誰能告訴我我怎麼會修改他們的「紙板」的方法(如下圖所示)發送一個簡單的內容「dot.txt」文件?

調用者要求您發送某種類型的數據。如果您可以提供該類型的數據,請將其放在粘貼板上。如果你不能,不要做任何事情。

+0

謝謝,彼得。那麼如果我試圖爲文本文件(字符串類型的數據)提供一個文本文件而不是圖像文件,這種方法仍然可以不被修改嗎? – Bender 2010-01-11 01:17:05

+0

閱讀並參閱。請求文本時它會做什麼? – 2010-01-11 01:26:23

+0

在輸入此方法之前,您並未向數據板提供數據 - 這是此方法的工作。這種方法實現了你之前做出的承諾;當另一端嘗試訪問您承諾但未提供的數據時,您會收到此消息。 – 2010-01-11 01:28:34