我如何将文本字段单元格(在可可中)中的文本复制到 NSPasteboard?

发布于 2024-09-12 18:54:26 字数 1035 浏览 8 评论 0原文

我在可可中有一个文本字段单元格和一个按钮。 我想通过单击按钮复制文本字段中的文本。

在clipboard.h中

 #import <Cocoa/Cocoa.h>


 @interface clipboard:NSObject {
  IBOutlet id but1;
  IBOutlet id numf2_1;
  NSPasteboard *pasteBoard;
  }
    - (BOOL) writeToPasteBoard:(NSString *)stringToWrite;
    - (NSString *) readFromPasteBoard;
    - (id) init;
    //- (IBAction) insert_cb:(id)sender;
 @end

在clipboard.m中

 #import "clipboard.h"
 //@implementation clipboard
 @implementation clipboard
  //- (IBAction) insert_cb:(id)sender{

 - (id) init
  {
    [super init];
     pasteBoard = [NSPasteboard generalPasteboard];
     return self;
  }

  - (BOOL) writeToPasteBoard:(NSString *)stringToWrite
  {

   [pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
    return [pasteBoard setString:stringToWrite forType:NSStringPboardType];
   }

   - (NSString *) readFromPasteBoard
  {
   return [pasteBoard stringForType:NSStringPboardType];
  }
  @end

我怎样才能改变它来做到这一点?

I have a textfield cell and a push button in the cocoa .
I want to copy the text in teh textfield by clicking on the button.

in clipboard.h

 #import <Cocoa/Cocoa.h>


 @interface clipboard:NSObject {
  IBOutlet id but1;
  IBOutlet id numf2_1;
  NSPasteboard *pasteBoard;
  }
    - (BOOL) writeToPasteBoard:(NSString *)stringToWrite;
    - (NSString *) readFromPasteBoard;
    - (id) init;
    //- (IBAction) insert_cb:(id)sender;
 @end

in clipboard.m

 #import "clipboard.h"
 //@implementation clipboard
 @implementation clipboard
  //- (IBAction) insert_cb:(id)sender{

 - (id) init
  {
    [super init];
     pasteBoard = [NSPasteboard generalPasteboard];
     return self;
  }

  - (BOOL) writeToPasteBoard:(NSString *)stringToWrite
  {

   [pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
    return [pasteBoard setString:stringToWrite forType:NSStringPboardType];
   }

   - (NSString *) readFromPasteBoard
  {
   return [pasteBoard stringForType:NSStringPboardType];
  }
  @end

How can i change this to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒猫 2024-09-19 18:54:26

您实际上从未向自己发送 writeToPasteboard: 消息,因此您需要这样做。您需要向文本字段询问其字符串值,并将其作为参数传递给 writeToPasteboard: 消息。

更好的是,只需将按钮连接到第一个响应者的 copy: 操作即可。文本字段响应此消息,因此只要它是第一个响应者(或至少在响应者链上足够高),操作消息就会到达该字段,并且该字段将自行复制文本。有关详细信息,请参阅 Cocoa 事件处理指南

这就是主菜单中的“复制”菜单项的工作原理,并且您无需编写任何代码即可实现菜单项或按钮。

You never actually send yourself a writeToPasteboard: message, so you need to do that. You'll want to ask the text field for its string value and pass that as the argument to the writeToPasteboard: message.

Better yet, just wire the button to the first responder's copy: action. The text field responds to this message, so as long as it is the first responder (or at least high enough up the responder chain), the action message will hit the field and the field will copy the text on its own. See the Cocoa Event-Handling Guide for more information.

That's how the Copy menu item in your main menu already works, and it's how you don't need to write any code to implement either the menu item or the button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文