Search code examples
objective-ccocoamacosnsviewnsbutton

Why does this code not work?


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"]];

Solution

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