2010-10-15 49 views
4

我想在我的UITextView的UIViewController中处理粘贴事件,这样如果粘贴的数据是文本或HTML,它应该像平常一样粘贴数据,因为它已经做到了,但如果它是其他类似图像或PDF或其他任何东西,我想以另一种方式在本地存储数据,而不是粘贴到UITextView(因为它不能这样做)。在UIViewController中处理粘贴事件

我该如何解决这个问题?在我能从Apple找到的唯一例子中,他们知道他们粘贴的数据的类型。甚至让我的控制器中的“ - (void)paste:(id)sender”方法在我的UITextView中发生粘贴事件时被调用,以此逃脱我。

谢谢

回答

1

您可以使用该方法canReadObjectForClasses:options:测试的东西,而无需实际检索值。

例如,在测试字符串:

NSArray *classes = [NSArray arrayWithObject:[NSString class]]; 
NSDictionary *options = [NSDictionary dictionary]; 

if ([[NSPasteboard generalPasteboard] canReadObjectForClasses:classes options:options]) { 
    NSLog(@"yup, there is a string in here"); 
} 

更新:

你检查在粘贴板中的所有对象这种方式,只需添加您所期望的阵列上的任何类。

NSArray *classes = [NSArray arrayWithObjects:[NSURL class], [NSString class], nil]; 

使用UIResponderStandardEditActions协议实现粘贴方法。

+0

检查纸板上的所有UTI会不会更好?如何捕获粘贴方法调用? – Neigaard 2010-10-15 11:58:26

+0

我刚刚更新了答案:) – pablasso 2010-10-15 12:14:04