How to peel / peel cardboard on viewWillDisappear

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

The code is as follows:

 - (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"); } } 

And even this help cannot (NSLog returns: pasteb2 (null) )

 -(void) viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [pasteBoard setString:@""]; } 
+6
source share
3 answers

iOS - UIPasteboard

Try the following:

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

Arab_Geek's answer is correct, but available for Cocoa (and I suspect you're looking for an iOS solution)

+19
source

OS X - NSPasteboard

Here you go ..

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

Setting "" will return nil for all intended purposes. However, it will leave the cardboard in a slightly different condition, as before the insertion operation.

Swift

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

... is not equivalent to UIPasteboard.removePasteboardWithName() . If restoring the state of a UIPasteboard is a 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) State recovery.

+2
source

Source: https://habr.com/ru/post/918261/


All Articles