samirah
samirah

Reputation: 199

Possible to check if nib is already loaded?

In a loop i am using the following code to load the preferences window if the user has not entered his/her settings.

[NSBundle loadNibNamed:@"prefs" owner:self];

My problem is that everytime the loop runs a new window opens again and again until, is there any way to check if the window or nib is already loaded once?

Thanks! :)

Upvotes: 1

Views: 660

Answers (2)

Rob Keniger
Rob Keniger

Reputation: 46020

Rather than micro-managing the nib loading as you are doing, just use a subclass of NSWindowController to handle the preferences window. NSWindowController handles all the nib management for you, you just need to call -showWindow: to display the window. The only real trick with NSWindowController is to make sure you hook up the window outlet of File's Owner in the nib itself.

To instantiate the class, use ‑initWithWindowNibName: passing in the name of the nib.

As Ief2 mentioned, you should configure this object to act as a singleton.

Upvotes: 4

v1Axvw
v1Axvw

Reputation: 3054

Maybe you can make a class named PreferencesController, make that class load the nib when requested, save the the window in an ivar. Every time you create an instance of the class and activate it, check if the window ivar is nil. If it is, load the nib, otherwise just make the window key and order front.

In addition to storing the window, you could also monitor it for when the user closes it. When he or she does, you can release your ivar and ser it to nil. Now when you request it again, you'll see you have no window cached and you'll have to reload the nib. It saves memory though.

It also may be advised to make a shared instance of the class. I cannot include a sample because I'm on my iPod touch, but a quick Google search should be really helpful.

Hope it helps, ief2

EDIT: The link below holds an example about singletons:

http://eschatologist.net/blog/?p=178

Upvotes: 1

Related Questions