使用IB连接NSImageView进行查看?

时间:2011-02-23 05:09:55

标签: objective-c cocoa xcode macos

到目前为止,我只在iPhone上进行了编程,因此Cocoa在某些方面对我来说有点混乱。这是我遇到麻烦的地方。我想要我的窗口,使背景看不见,没有标题栏。像这样:

enter image description here

以下是我的表现:

我将window的类设置为自定义窗口,我创建了这样的窗口:

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

现在,在此窗口的.xib文件中,我已在窗口中添加了自定义视图。我已将视图类设置为我创建的自定义类,该类继承自NSView。以下是我如何设置它:

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

所以这是我的问题。我在Interface Builder中向自定义视图(NSImageView)添加了MainView。但是,出于某种原因,我无法弄清楚如何将此图像视图连接到我的自定义视图中的实例变量。他们似乎无法像我通常在创建iPhone应用程序时那样连接。有任何想法如何做到这一点?

1 个答案:

答案 0 :(得分:1)

在Mac OS X中以与iOS程序相同的方式连接在XIB中创建的对象。只需将NSImageView属性添加到主视图中,将其标记为IBOutlet并将其连接起来。

例如,

在MainView.h中为NSImageView创建一个属性并使其成为IBOutlet:

#import <Cocoa/Cocoa.h>

@interface MainView : NSView {
  NSImageView *imageView;
}

@property(retain) IBOutlet NSImageView *imageView;

@end

在界面构建器中,确保将自定义视图的类设置为MainView,单击自定义视图XIB中的File's Owner对象,然后在检查器中选择identity选项并输入MainView作为类类型

接下来,按CTRL +单击文件的所有者,然后将箭头拖到NSImageView并选择imageView插座。

这就是它的全部。您现在应该能够从代码中引用图像视图。