繁体   English   中英

在iPhone上复制并粘贴多个数据表示

[英]Copy and Paste on iPhone with multiple data representations

尝试将多个数据表示放在iPhone 3.0上的粘贴板上时遇到了一些问题。

我要做的是将数据表示和字符串表示放在粘贴板上。 数据是我自己的数据类型,我用它来复制和粘贴我的应用程序。 字符串表示是一种将我的应用程序内容作为大纲复制并粘贴到其他应用程序(例如Mail.app)的方法。

    // payload
NSString *pasteboardString = [selectedNode stringRepresentation];
NSDictionary *pasteboardDictionary = [selectedNode nodeAndSubnodesProperties];

// set payload
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = pasteboardString;
[pasteboard setValue:pasteboardDictionary forPasteboardType:MNTNodesPasteboardType];

上面的代码不起作用,因为string属性和setValue:forPasteboardType:methode替换了粘贴板上的第一个表示。 我尝试了addItems:但它对我不起作用。

感谢您的任何帮助!

回答我自己的问题:

您必须使用items属性将多个表示放入粘贴板。 为此,您需要创建一个字典,其中每个表示作为值,表示类型作为键。 将此字典添加到数组中,其中数组中的每个项目表示一个项目(UIPasteboard支持向粘贴板添加多个项目以及向每个项目添加多个表示)。

具有两个表示的单个项的示例代码:

    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSMutableDictionary *item = [NSMutableDictionary dictionaryWithCapacity:2];
[item setValue:[NSKeyedArchiver archivedDataWithRootObject:pasteboardDictionary] forKey:MNTNodesPasteboardType];
[item setValue:pasteboardString forKey:(NSString *)kUTTypeUTF8PlainText];
pasteboard.items = [NSArray arrayWithObject:item];

不要忘记链接MobileCoreServices框架来解析UTI常量。

这对我来说在Swift中有用,它将图像和文本一起粘贴到贴板上

let pastebaord = UIPasteboard.generalPasteboard()
let image = UIImage(named: "my-image-file")
pastebaord.addItems([ [UIPasteboardTypeListString[0] as! String : "hello"], [UIPasteboardTypeListImage[0] as! String: image!]]) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM