我使用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:@""];
}发布于 2012-06-26 23:53:03
iOS - UIPasteboard
尝试以下操作:
UIPasteboard *pb = [UIPasteboard generalPasteboard];
[pb setValue:@"" forPasteboardType:UIPasteboardNameGeneral];阿拉伯极客的回答是正确的,但可用于Cocoa (我怀疑您正在寻找iOS解决方案)
发布于 2012-06-17 07:58:47
OS X- NSPasteboard
给你..。
NSPasteboard *pb = [NSPasteboard generalPasteboard];
[pb declareTypes: [NSArray arrayWithObject:NSStringPboardType] owner: self];
[pb setString: @"" forType: NSStringPboardType];发布于 2015-08-28 00:39:33
将该值设置为""将返回用于所有预期目的的nil。但是,它将使粘贴板处于与粘贴操作之前略有不同的状态。
Swift
let pb = self.pasteBoard()
pb.setValue("", forPasteboardType: UIPasteboardNameGeneral)...is不等同于UIPasteboard.removePasteboardWithName()。如果需要恢复UIPasteboard状态(1),您可以使用以下块:
Swift
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)恢复状态。
https://stackoverflow.com/questions/11067554
复制相似问题