How to clear/empty pasteboard on viewWillDisappear

元气小坏坏 提交于 2019-11-29 07:19:46
Riddick

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)

AK_

OS X - NSPasteboard

Here you go ..

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!