views:

490

answers:

1

I am trying to load a Nib from a C++ constructor with Objective C++. I have searched and found enough examples to get some code together but it doesn't display the window. Or any windows for that matter.

Here is an example of the contructor:

JokeSystem::JokeSystem() : fileSystem("/Library/Application Support/Jokes/")
{
    try
    {
     randSystem = new RandSelect<std::string>
                                   (fileSystem.getAllFileContents("%\n"));
    }
    catch (std::ifstream::failure)
    {
     NSWindowController * errorWindowControl = [[NSWindowController alloc] 
                                        initWithWindowNibName:@"ErrorWindow"];
     [errorWindowControl showWindow: nil];
    }
}

The purpose of the contructor is to load the contents of a directory into a string. What I am try to do is display the error window when the files fail to open.

ErrorWindow.nib has a single window with an OK button and a NSTextView for the error, I set up a NSWindowController in the nib and connected it to the window.

My only link has been that most examples show this [errorWindowControl showWindow: self]; rather than showWindow: nil but because this is a C++ constructor I it doesn't have self and this doesn't work.

If it matters this contructor is called from the awakeFromNib method of the MainMenu.nib's primary NSObject.

Thanks

+1  A: 

A bit of an odd way to approach Cocoa. I would encourage you to step back, learn Objective-C and then write your application with an Objective-C based Cocoa UI layer on top of whatever backing store or model layer you have.

In any case, there isn't anything particularly wrong with that code (save for the odd design).

The first thing to check is the return value of -initWithWindowNibName:. Is errorWindowControl actually non-nil? If it is nil, then the NIB failed to load.

How are you writing the Cocoa application itself? Standard app bundle using Xcode, I hope?

Also, you shouldn't be hardcoding the path to /Library/Application Support/. Actually, your application shouldn't use that directory as the only storage location; many users won't have write access to that directory and won't be able to install your app without administrator access.

bbum
I tested errorWindowControl, and it wasn't nil. The Objective-C that is in the program is a front end for the MainMenu.nib and runs a c++ function from this class. That is why I am creating the window here, because the above constructor is used on the start of the program, and this displays a window and quits the program. The directory is ~/Library/Application Support but I used this to load the incorrect directory to check my error handling. I am using a standard Cocoa app bundle in Xcode. Thanks