PDA

View Full Version : Document Based




davidlt
Jun 30, 2008, 09:10 AM
Hi, I can't figure out this.
I have MyDocument class which inherits from NSDocument and DocView which intherits from NSView. DocView has IBOutlet to MyDocument. When I working inside DocView and I want to get some information from my MyDocument object, I get "null".

DocView.h:
#import <Cocoa/Cocoa.h>
@class MyDocument;

@interface DocView : NSView {
IBOutlet MyDocument *doc;
}
@end

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

@interface MyDocument : NSDocument
{
}
- (BOOL)rand;
@end

And let's say code in DocView will be like this:
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
}

NSLog(@"Rand: %@", [doc rand]);
...
...

So I launch my application, press File, Open, I will choose my last saved file, MyDocument becomes initialized and it's time to initialize docView and it should write "Rand: YES" into the Log (rand always gives YES in this example), but I get "Rand: (null)". So why can't I contact my MyDocument?

In Interfeice Builder there is connection from CustomView (DocView) to File's Ower (MyDocument).



yoavcs
Jun 30, 2008, 09:40 AM
I believe the nib loader first fully initializes all custom view objects archived in a nib before setting their outlets and other instance variables.

This would mean that your custom view doesn't yet have its outlet to MyDocument set inside its initializer. Which is where you are trying to use it, thus resulting in NULL.

Note, this should only be happening with custom views. Other objects are already initialized before ever being archived into the nib.

I may be wrong though.

davidlt
Jun 30, 2008, 10:45 AM
I am going to check that a bit later and write if it's true. This should mentioned somewhere in documentation, I will try to find it.

Found that: http://developer.apple.com/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/chapter_3_section_3.html#//apple_ref/doc/uid/10000051i-CH4-DontLinkElementID_7

So it works as you said. Thanks.