Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

davidlt

macrumors member
Original poster
May 22, 2007
56
0
Lithuania
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

macrumors regular
Apr 7, 2004
220
96
Israel
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.