如何在viewWillDisappear清除/清空粘贴板

我使用UIPasteboard在两个UITextView之间复制/粘贴文本。

代码如下所示:

 - (void)viewDidLoad { [super viewDidLoad]; pasteBoard = [UIPasteboard generalPasteboard]; //it is declared in .h as UIPasteboard *pasteBoard; } -(IBAction)doCopyBtn { if (![toCopyTextView.text isEqualToString:@""]){ pasteBoard.string = toCopyTextView.text; NSLog(@"pasteb1 %@", pasteBoard.string); } else { NSLog (@"error! enter smth"); } } -(IBAction)doPasteBtn { if (![pasteBoard.string isEqualToString:@""]){ toPasteTextView.text = pasteBoard.string; NSLog(@"pasteb2 %@", pasteBoard.string); } else { NSLog (@"error! enter smth"); } } 

甚至这不能帮助(NSLog返回: pasteb2 (null)

 -(void) viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [pasteBoard setString:@""]; } 

iOS – UIPasteboard

尝试以下操作:

  UIPasteboard *pb = [UIPasteboard generalPasteboard]; [pb setValue:@"" forPasteboardType:UIPasteboardNameGeneral]; 

Arab_Geek的反应是正确的,但可用于cocoa(我怀疑你正在寻找一个iOS解决scheme)

OS X – NSPasteboard

干得好 ..

 NSPasteboard *pb = [NSPasteboard generalPasteboard]; [pb declareTypes: [NSArray arrayWithObject:NSStringPboardType] owner: self]; [pb setString: @"" forType: NSStringPboardType]; 

将该值设置为""将返回nil为所有预期的目的。 但是,这将使粘贴板处于与粘贴操作之前稍微不同的状态。

迅速

 let pb = self.pasteBoard() pb.setValue("", forPasteboardType: UIPasteboardNameGeneral) 

…不等同于UIPasteboard.removePasteboardWithName() 。 如果需要恢复UIPasteboard状态(1),则可以使用以下块:

迅速

 let pb = self.pasteBoard() let items:NSMutableArray = NSMutableArray(array: pb.items) for object in pb.items { if let aDictionary = object as? NSDictionary { items.removeObject(aDictionary) } } pb.items = items as [AnyObject] 

(1)恢复状态。