iOS - 检测用户何时复制到剪贴板 - [UIPasteboard generalPasteboard] [英] iOS - detect when user copy to clipboard - [UIPasteboard generalPasteboard]
问题描述
快速简单的问题
在使用带有一些文字的WebView时 - 用户可以从中选择一段文本
并按下UIButton我创建 - 运行以下操作:
while using a WebView with some text in it - the user can select a snippet of text from it and press a UIButton which I created - running the following action:
-(IBAction)copyToClip
{
NSString *copyClip = [UIPasteboard generalPasteboard].string;
NSLog(@"Clip = %@",copyClip);
// (works fine)
}
我想打电话给没有UIButton的相同功能,因此当用户执行复制操作时,它将激活上述代码。 (我假设有一个听众)
I would like to call the same function without a UIButton, thus when the user did a "copy" action it will activate the above code. (I assume a listener)
这对于什么是合适的听众?
what would be the appropriate listener for this?
推荐答案
使用NSNotificationCenter并注册UIPasteboardChangedNotification:
http://developer.apple.com/library/IOs/documentation/UIKit/Reference/UIPasteboard_Class/Reference.html#//apple_ref/c/data/UIPasteboardChangedNotification
Use NSNotificationCenter and register for UIPasteboardChangedNotification: http://developer.apple.com/library/IOs/documentation/UIKit/Reference/UIPasteboard_Class/Reference.html#//apple_ref/c/data/UIPasteboardChangedNotification
[[NSNotificationCenter defaultCenter] addObserver:object selector:@selector(copyToClip) name:UIPasteboardChangedNotification object:nil];
这篇关于iOS - 检测用户何时复制到剪贴板 - [UIPasteboard generalPasteboard]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!