如何在viewWillDisappear上清除/清空粘贴板

Ale*_*pov 6 cocoa-touch uitextview uipasteboard ios

我用它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");
    }
}
Run Code Online (Sandbox Code Playgroud)

即使这不能帮助(的NSLog返回:pasteb2 (null))

-(void) viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [pasteBoard setString:@""]; 
}
Run Code Online (Sandbox Code Playgroud)

小智 20

iOS - UIPasteboard

请尝试以下方法:

    UIPasteboard *pb = [UIPasteboard generalPasteboard];
    [pb setValue:@"" forPasteboardType:UIPasteboardNameGeneral];
Run Code Online (Sandbox Code Playgroud)

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