title: Can I pass the a reference of an NSWindow to a custom object and use that object to add an NSButton and Action?
body: <p>I was wondering if it's possible to pass the reference of an NSWindow to a custom object and then use that object to to add an NSButton and associated action/selector for that button.</p>
  
  <p>I seem to be running into issues when I attempt this.  When I run the sample program and click on the button, the following runtime error occurs:
  Thread 1: EXC_BAD_ACCESS (code=1, address=0x18)</p>
  
  <p>Here is my code:</p>
  
  <pre><code>//  AppDelegate.h
  
  #import &lt;Cocoa/Cocoa.h&gt;
  
  @interface AppDelegate : NSObject &lt;NSApplicationDelegate&gt;
  @property (assign) IBOutlet NSWindow *window;
  @end
  
  //  AppDelegate.m
  
  #import "AppDelegate.h"
  #import "CustomObject.h"
  @implementation AppDelegate
  
  - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  {
      CustomObject *test = [[CustomObject alloc]init];
      [test createButton:_window];
  }
  @end
  
  
  //  CustomObject.h
  
  #import &lt;Foundation/Foundation.h&gt;
  
  @interface CustomObject : NSObject
  {
      int test;
      NSButton *testButton;
  }
  - (IBAction)pressCustomButton:(id)sender;
  
  -(void)createButton:(NSWindow*)win;
  @end
  
  //  CustomObject.m
  
  #import "CustomObject.h"
  
  @implementation CustomObject
  
  -(IBAction)pressCustomButton:(id)sender
  {
      NSLog(@"pressCustomButton");
  }
  
  -(void)createButton:(NSWindow*)win
  {
      testButton = [[NSButton alloc] initWithFrame:NSMakeRect(100, 100, 200, 50)];
  
      [[win contentView] addSubview: testButton];
      [testButton setTitle: @"Button title!"];
      [testButton setButtonType:NSMomentaryLightButton]; //Set what type button You want
      [testButton setBezelStyle:NSRoundedBezelStyle]; //Set what style You want
  
      [testButton setTarget:self];
      [testButton setAction:@selector(pressCustomButton:)];
  }
  @end
  </code></pre>