Search code examples
objective-cxcodemacosnsimage

[NSImage setHidden:]: unrecognized selector sent to instance


macOS Xcode Objective-C. Very new, so bear with me.

Displaying an image programmatically using:

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        m_imgBack = nil;
}

- (void)drawRect:(NSRect)rect {
    rect = [ self bounds ];
    if( m_imgBack )
      [m_imgBack drawInRect:NSMakeRect(0,0,rect.size.width,rect.size.height) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
}

- (void)InitControls {
    LoadImageFromFile( [ NSString stringWithFormat:@"%@/common/background.png", DATA_PATHNAME ], &m_imgBack );
}

This draws the background.png without a problem. However, I can not see to hide it using:

[m_imgBack setHidden:YES];

It just kicks back error: [NSImage setHidden:]: unrecognized selector sent to instance. I thought you could sethidden on an NSImage?


Solution

  • You can think of NSImage as a data object, so hiding it doesn't make sense just like hiding an NSArray or NSDictionary doesn't make sense.

    If you're displaying the image in a view, the view can be hidden using isHidden. It's not clear from the code snippet what class the code belongs to, but assuming it's a subclass of NSView, you could try [self setHidden:YES].