-2

So I have the following code to create an NSButton, and put it on the screen. However, when I run this code, nothing happens. What is wrong with this?

NSButton *newbutton = [[NSButton alloc] initWithFrame:NSMakeRect(100, 100, 109, 151)];
    [newbutton setImage:[NSImage imageNamed:@"4Diamonds.png"]];
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Regan
  • 1,487
  • 2
  • 28
  • 43

1 Answers1

6

The button doesn't appear magically by itself. You have to add it to a visible view, e.g. by using -[NSView addSubView:].

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • Hmmm, alright thanks, I'm having trouble connecting my main background view to an outlet so I can add subviews to it, is there a reason for this? – Regan Nov 09 '10 at 03:40
  • @Regan: You'd have to provide some more detail as to what is not working. – Georg Fritzsche Nov 09 '10 at 03:45
  • I am not sure how to reference programmatically the superview that I want to add my subview to. – Regan Nov 09 '10 at 04:03
  • 1
    @Regan: I'm guessing that using a nib file to set up your user interface would be beneficial. You could then (if you really want to add the button programmatically) create an IBOutlet instance variable and hook that up to the superview from within Interface Builder. – NSGod Nov 09 '10 at 04:17
  • In a generated Cocoa project (not document based) you can quickly test it by using `[window contentView]` in the app delegate. For anything other than testing reading through a tutorial on how to work with Interface Builder et al will be very helpful. – Georg Fritzsche Nov 09 '10 at 04:29