6

我用来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:@""]; 
}
4

3 回答 3

22

iOS –UIPasteboard

尝试以下操作:

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

Arab_Geek 的回答是正确的,但可用于 Cocoa(我怀疑您正在寻找 iOS 解决方案)

于 2012-06-26T15:53:02.930 回答
3

操作系统 -NSPasteboard

干得好 ..

NSPasteboard *pb = [NSPasteboard generalPasteboard];
[pb declareTypes: [NSArray arrayWithObject:NSStringPboardType] owner: self];
[pb setString: @"" forType: NSStringPboardType];
于 2012-06-16T23:58:46.840 回答
2

将值设置为""将返回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) 恢复状态。

于 2015-08-27T16:39:33.130 回答