How to clear/empty pasteboard on viewWillDisappear

后端 未结 3 1476
时光说笑
时光说笑 2020-12-18 03:38

I use UIPasteboard to copy/paste text between two UITextView.

Code looks like this:

- (void)viewDidLoad {
   [super viewDid         


        
相关标签:
3条回答
  • 2020-12-18 03:56

    Setting the value to "" will return nil for all intended purposes. It will, however, leave the pasteboard in a slightly different state as before a paste operation.

    Swift

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

    ...is not equivalent to UIPasteboard.removePasteboardWithName(). If restoring the UIPasteboard state is of concern(1), you can use the following block:

    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) Restoring state.

    0 讨论(0)
  • 2020-12-18 04:08

    OS X - NSPasteboard

    Here you go ..

    NSPasteboard *pb = [NSPasteboard generalPasteboard];
    [pb declareTypes: [NSArray arrayWithObject:NSStringPboardType] owner: self];
    [pb setString: @"" forType: NSStringPboardType];
    
    0 讨论(0)
  • 2020-12-18 04:13

    iOS – UIPasteboard

    Try the following:

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

    Arab_Geek's response is correct but available for Cocoa (and I suspect you are looking for an iOS solution)

    0 讨论(0)
提交回复
热议问题