如何在 iPhone 中以编程方式执行复制/粘贴功能?
我有一个文本视图,其中包含一些文本和该视图中的复制按钮,
当用户输入一些文本并按下复制按钮时,它需要复制该文本并将该文本粘贴到他想要的任何地方。
我知道 iOS 中有一个默认的复制/粘贴菜单控制器,但我想通过单击按钮来执行此功能。我认为有 UIPasteboard
可以实现此功能,但我不知道如何使用它。
I have a text-view with some text and a copy button in that view,
When the user enters some text and presses the copy button, it needs to copy that text and paste that text wherever he wants.
I know there is a default copy/paste menu-controller in iOS, but I want to do this functionality in a button click. I think there is UIPasteboard
to do this functionality, but I don't know how to use it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要从按钮复制,请单击:
要从按钮粘贴,请单击:
To copy from a button click:
To paste from a button click:
Swift
这是已接受答案的 Swift 版本。
复制
粘贴
Swift
This is the Swift version of the accepted answer.
Copy
Paste
对于使用 MonoTouch 的开发人员,以下是我在 C# 中用来完成任务的两行代码。
为这个问题提供的答案 iscavenger 作为我答案的模型(在我在我的项目中成功实现它之后;-)
For developers using MonoTouch, here are the two lines I used to complete the task in C#.
The answer iscavenger provided to this question served as the model for my answer (after I successfully implemented it in my project ;-)
不知道为什么我们不能简单地使用:
按照 UIResponder 文档
Not sure why we can't simply use:
as per UIResponder docs
我怀疑您可以相对轻松地做您想做的事情,从
[UIPasteboard dataForPasteboardType:]
方法。您可以查看 Apple 示例代码:
http://developer.apple.com/library/ios/#samplecode/CopyPasteTile/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009040
I suspect you can relatively easily do what you want, starting with the
[UIPasteboard dataForPasteboardType:]
method.There's Apple sample code you can look into at:
http://developer.apple.com/library/ios/#samplecode/CopyPasteTile/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009040