1
votes

I have tried many possibilities I found on this site and read some explanations from apple developer page, but seems like i couldn't resolve my problem about loading NSViewController with/form NIB.

Files on Xcode Project look a bit like this :

  • SecondViewController.h
  • SecondViewController.m
  • SecondViewController.xib
  • AppDelegate.h
  • AppDelegate.m
  • MainMenu.xib

The main problem is how could i create the SecondViewController programatically with the initial nib on SecondViewController.xib

  • Custom class of FileOwner on MainMenu.xib is NSApplication
  • Custom class of FileOwner on SecondViewController.xib is SecondViewController
  • There are some panels and window in MainMenu.xb (about window and preference panel)
  • This application has no main window (using notification icon on status bar)

SecondViewController.h

@interface SecondViewController : NSViewController {
    BOOL fullScreenMode;
    NSImageView *fullScreenbg;
    NSImageView *imageView1;
    NSImageView *imageView2;
    NSPanel *imageWindow;

}


@property (assign) IBOutlet NSImageView *fullScreenbg;
@property (assign) IBOutlet NSImageView *imageView1;
@property (assign) IBOutlet NSImageView *imageView2;
@property (assign) IBOutlet NSPanel *imageWindow;

SecondViewController.m

@implementation SecondViewController {
    NSImageView *nv1;
    NSImageView *nv2;
    NSSize curImgSize;
}

@synthesize fullScreenbg;
@synthesize imageView1;
@synthesize imageView2;
@synthesize imageWindow;

......

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.
        fullScreenMode = YES;
    }

    return self;
}

AppDelegate.h

@interface AppDelegate : NSObject <NSApplicationDelegate,NSWindowDelegate> {
   NSPanel *aboutWindow;
   IBOutlet NSMenu *myStatusMenu;
   IBOutlet NSMenuItem *toggleFullScreen;
}

AppDelegate.m

@implementation AppDelegate {
    SecondViewController *controller;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    controller = [[SecondViewController alloc] 
                 initWithNibName:@"SecondViewController" 
                 bundle:nil];  
    //Not work (fullscreenbg, imageView1, imageView2,imageWindow = nil)

    //controller = [[SecondViewController alloc] init]; ?? <-- didn't work either
}

Even if using initWithNibName or just init, all the IBOutlet properties seems to be nil on debug.

i've tried other solustions like "NSBundle loadNibNamed" or using loadView but it didn't work (warning message : "NSObject my not respond to loadView").

The main purpose of the secondViewController is to display notification message including graphics and web element.

I hope someone could give me a best suggestion. Thanks.

2
Set a breakpoint during your NSViewController's initialisation and see if you're actually initialising it or not.TheAmateurProgrammer
Sure. The initialisation on appdelegate executed, initwithnibname at second controller executed, controller is not nil/null but all controller outlets are nil/null.theodorusap
Only outlets? Are you manually setting the outlets? If you are, try set them in awakeFromNib instead.TheAmateurProgrammer
I got it, add [NSBundle loadNibNamed:@"SecondViewController" owner:self]; before the line [super initWithNibName .......]theodorusap

2 Answers

0
votes

This is normal behavior. IBOutlets are not connected in the constructor.

You can override viewDidLoad, call super and then do any initialization.

-1
votes
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    //added this line :
    if (nibBundleOrNil!=nil || ![nibBundleOrNil isEqualtoString:@""]) {
        [NSBundle loadNibNamed:@"SecondViewController" owner:self];
    {

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.
        fullScreenMode = YES;
    }

    return self;
}