我如何将文本字段单元格(在可可中)中的文本复制到 NSPasteboard?
我在可可中有一个文本字段单元格和一个按钮。 我想通过单击按钮复制文本字段中的文本。
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上从未向自己发送
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 thewriteToPasteboard:
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.