sudo rm -rf
sudo rm -rf

Reputation: 29524

Connect NSImageView to view using IB?

I've only programmed on the iPhone so far, so Cocoa is sort of confusing in certain ways for me. Here's where I've hit a snag. I wanted my window so that the background was invisible, and without a title-bar. Something like this:

enter image description here

Here's how I'm doing it:

I set my window's class to a custom window, which I've created like this:

CustomWindow.h

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>


@interface CustomWindow : NSWindow {
    @private
    NSPoint initialLocation;
}

@property(assign)NSPoint initialLocation;

@end

CustomWindow.m

//trimmed to show important part
#import "CustomWindow.h"

@implementation CustomWindow
@synthesize initialLocation;

- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag {
    // Removes the window title bar
    self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
    if (self != nil) {
        [self setAlphaValue:1.0];
        [self setOpaque:NO];
    }
    return self;
}

@end

Now, in my .xib file for this window I've added a custom view onto the window. I've set the view class to a custom class I've created that inherits from NSView. Here's how I'm setting that up:

MainView.h

#import <Cocoa/Cocoa.h>

@interface MainView : NSView {
@private
//nothing to see here, add later
}

@end

MainView.m

//trimmed greatly again to show important part
#import "MainView.h"

@implementation MainView

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

    return self;
}

- (void)drawRect:(NSRect)rect {
    // Clear the drawing rect.
    [[NSColor clearColor] set];
    NSRectFill([self frame]);

}

@end

So here's my question. I've added a NSImageView to my custom view (MainView) in Interface Builder. However, for some reason I can't figure out how to connect this image view to an instance variable in my custom view. They seem like they can't be connected like I normally would if I was creating an iPhone app. Any ideas how this would be done?

Upvotes: 1

Views: 1767

Answers (1)

Cayden
Cayden

Reputation: 277

You connect objects created in your XIB in Mac OS X the same way you do for iOS programs. Just add an NSImageView property to your main view, mark it as an IBOutlet and connect it up.

For example,

In MainView.h create a property for your NSImageView and make it an IBOutlet:

#import <Cocoa/Cocoa.h>

@interface MainView : NSView {
  NSImageView *imageView;
}

@property(retain) IBOutlet NSImageView *imageView;

@end

In interface builder, make sure the class for the custom view is set to MainView, to do this click on the File's Owner object in the custom view XIB and then select the identity option in the inspector and enter MainView as the class type.

Next, CTRL+click File's owner and drag the arrow to the NSImageView and select the imageView outlet.

That's all there is to it. You should be able to reference the image view from code now.

Upvotes: 1

Related Questions