2012-09-22 100 views
0

我有一個名爲text的字符串,在主菜單的xib文件中,我將副本菜單項與通過observer調用的操作鏈接起來。我通過斷點驗證了此方法實際上是否被調用,但問題是,字符串是不是真的複製到剪貼板:它無法將字符串複製到粘貼板

- (void) copy: (NSNotification*) notification 
{ 
    if([[self window]isKeyWindow]) 
    { 
     // It always enters in this block 
     NSPasteboard* pb=[NSPasteboard generalPasteboard]; 
     NSPasteboardItem* item=[[NSPasteboardItem alloc]init]; 
     [pb clearContents]; 
     [item setData: [NSKeyedArchiver archivedDataWithRootObject: text] forType: NSPasteboardTypeString]; 
     [pb writeObjects: [NSArray arrayWithObject: item]]; 
    } 
} 

塊中進入明確所有的pasteboard.But的內容,如果我嘗試複製的內容到文本編輯粘貼後,它不會粘貼任何東西(一個空字符串),但字符串不是長度爲零。
我也試着檢查writeObjects的返回值,它返回YES。

回答

2

您可以簡單地使用:

[pb setString:text forType:NSPasteboardTypeString]; 

或者你可能想在NSPasteboardItem使用setString:forType

相關問題