开发者

Cocoa/Obj-C - TextField to clipboard button

Got 1 UITextField and 1 button. There is text in my textfield, and when we click on the button, the text is copied to the OSX clipboard.

How can I do that? I've readed the NSPastboard Class Reference but didn't understand how to do that -simply-

Got my button defined in my AppControler.h like this:

- (IBAction)copyBut开发者_JS百科ton:(id)sender;

What am I supposed to write in my AppControler.m? My textfield is called "descTextField"


- (IBAction)copyButton:(id)sender {
   NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
   [pasteBoard declareTypes:[NSArray arrayWithObjects:NSStringPboardType, nil] owner:nil];
   [pasteBoard setString: [textField stringValue] forType:NSStringPboardType];
}


according to apple's doc / guide i think it should be rather like this:

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


Hope this will work for you.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜