开发者

Set string to pasteboard (copy, paste) in cocoa application

How can i set a string so the 开发者_开发技巧user can paste it somewhere else in a cocoa objective c application?


[pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
[pboard setString:@"Unbelievable" forType:NSStringPboardType];

If you are targeting OS X 10.6 and higher only, use NSPasteboardTypeString instead of NSStringPboardType.


See the Pasteboard Programming Guide. You specifically want the section on "Copying to a Pasteboard."


[[NSPasteboard generalPasteboard] declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
[[NSPasteboard generalPasteboard] setString:@"My Text" forType:NSStringPboardType];

reference


This is the way you do if you're targeting OSX 10.6 or higher:

NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
[pasteboard writeObjects:@[@"Some string"]];

For more information see the Pasteboard Programming Guide on Apple's site.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜