Cocoa / Obj-C - TextField button on the clipboard

Get 1 UITextField and 1 button. There is text in the text box, and when we click on the button, the text is copied to the OSX clipboard .

How can i do this? I read the link to NSPastboard , but did not understand how to do this - just -

Got my button defined in my AppControler.h as follows:

- (IBAction)copyButton:(id)sender; 

What should I write in my AppControler.m ? My text box is called "descTextField"

+6
clipboard objective-c xcode cocoa macos
source share
3 answers
 - (IBAction)copyButton:(id)sender { NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard]; [pasteBoard declareTypes:[NSArray arrayWithObjects:NSStringPboardType, nil] owner:nil]; [pasteBoard setString: [textField stringValue] forType:NSStringPboardType]; } 
+17
source share

according to apple doc / guide I think it should be something like this:

 - (IBAction)copyButton:(id)sender { NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard]; [pasteBoard clearContents]; [pasteBoard writeObjects:@[[textField stringValue]]]; } 
0
source share

Hope this works for you.

  - (IBAction)copyToClipboardActionBtn:(id)sender { UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard]; [pasteBoard setString: _descTextField.text]; } 
0
source share

All Articles