views:

22

answers:

1

hi, 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?

A: 

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.

Peter Hosey