Note that there are some explanatory texts on larger screens.

plurals
  1. POCocoa - loading a view from a nib and displaying it in a NSView container , as a subview
    text
    copied!<p>I've asked about this earlier but the question itself and all the information in it might have been a little confusing, plus the result i want to get is a little more complicated. So i started a new clean test project to handle just the part that im interested to understand for the moment.</p> <p>So what i want, is basically this: i have a view container (inherits NSView). Inside, i want to place some images, but not just simple NSImage or NSImageView, but some custom view (inherits NSView also), which itself contains a textfield and an NSImageView. This 'image holder' as i called it, is in a separate nib file (im using this approach since i am guiding myself after an Apple SAmple Application, COCOA SLIDES). The results i got so far, is something but not what i am expecting. Im thinking i must be doing something wrong in the Interface Builder (not connecting the proper thingies), but i hope someone with more expertise will be able to enlighten me.</p> <p>Below i'll try to put all the code that i have so far:</p> <pre><code>//ImagesContainer.h #import &lt;Cocoa/Cocoa.h&gt; @interface ImagesContainer : NSView { } @end //ImagesContainer.m #import "ImagesContainer.h" #import "ImageHolderView.h" #import "ImageHolderNode.h" @class ImageHolderView; @class ImageHolderNode; @implementation ImagesContainer - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code here. //create some subviews for(int i=0;i&lt;3;i++){ ImageHolderNode *node = [[ImageHolderNode alloc] init]; [self addSubview:[node rootView]]; } } NSRunAlertPanel(@"subviews", [NSString stringWithFormat:@"%d",[[self subviews] count]], @"OK", NULL, NULL); return self; } - (void)drawRect:(NSRect)dirtyRect { // Drawing code here. [[NSColor blackColor] set]; NSRectFill(NSMakeRect(0,0,dirtyRect.size.width,dirtyRect.size.height)); int i=1; for(NSView *subview in [self subviews]){ [subview setFrameOrigin:NSMakePoint(10*i, 10)]; i++; } } @end //ImageHolderView.h #import &lt;Cocoa/Cocoa.h&gt; @interface ImageHolderView : NSView { IBOutlet NSImageView *imageView; } @end //ImageHolderVIew.m #import "ImageHolderView.h" @implementation ImageHolderView - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if (self) { } return self; } - (void)drawRect:(NSRect)dirtyRect { // Drawing code here. [[NSColor blueColor]set]; NSRectFill(NSMakeRect(10,10, 100, 100)); //[super drawRect:dirtyRect]; } @end //ImageHolderNode.h #import &lt;Cocoa/Cocoa.h&gt; @class ImageHolderView; @interface ImageHolderNode : NSObject { IBOutlet ImageHolderView *rootView; IBOutlet NSImageView *imageView; } -(NSView *)rootView; -(void)loadUIFromNib; @end //ImageHolderNode.m #import "ImageHolderNode.h" @implementation ImageHolderNode -(void)loadUIFromNib { [NSBundle loadNibNamed:@"ImageHolder" owner: self]; } -(NSView *)rootView { if( rootView == nil) { NSRunAlertPanel(@"Loading nib", @"...", @"OK", NULL, NULL); [ self loadUIFromNib]; } return rootView; } @end </code></pre> <p>My nib files are:</p> <ol> <li>MainMenu.xib</li> <li><p>ImageHolder.xib</p></li> <li><p>MainMenu is the xib that is generated when i started the new project.</p></li> <li>ImageHolder looks something like this: <a href="http://www.pixavue.com/remoteimg/imageholder.png" rel="nofollow noreferrer">image link</a></li> </ol> <p>I'll try to mention the connections so far in the xib ImageHolder :</p> <p>File's Owner - has class of ImageHolderNode The main view of the ImageHolder.xib , has the class ImageHolderView</p> <p>So to resume, the results im getting are 3 blue rectangles in the view container, but i cant seem to make it display the view loaded from the ImageHolder.xib</p> <p>If anyone wants to have a look at the CocoaSlides sample application , its on apple developer page ( im not allowed unfortunately to post more than 1 links :) )</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload