• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Getting a NSView pointer from a nib
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting a NSView pointer from a nib


  • Subject: Re: Getting a NSView pointer from a nib
  • From: Daniel Braun <email@hidden>
  • Date: Fri, 28 Apr 2006 21:22:57 +0200


I was wondering today if is possible to retrieve a NSView* from a nib. For example I have a nib which just contains a custom view on which several controls are laid out. What I want to do is get a pointer to that view (instantiate the nib first) and make it the content view of another window (eg. the custom view could contain preferences controls for a preferences window). I'm also wondering how am I going to connect the separate nib with the rest of the application...

As already stated in other replies, you probably dont need to load nib at runtime at all, You probably want to have a single nib file (or one for the app, one for the document), and let bindings do the work for you. You may even want to ignore that custom view can exists :


If you really want to load NIB at runtime  however here is my code

This is done in an "Applet" : each applet (created e.g. at startup) has an associated view, stored in a nib file. When needed, the nib file is loaded and the view is added to a custom view (kind of black board where i drag and drop applets)

@interface Applet : CBBase {
	IBOutlet NSView *nibView;
	....
}
@end

@implementation Applet
...
- (NSView *) getView
{
	BOOL ok=[NSBundle loadNibNamed:@"applet2" owner:self];
	if (ok) return nibView;
	...
	return nil;
}
@end

In the NIB file (of course, here, named "applet2.nib"), set class of "file's owner" to Applet. Add your views, possibly related controllers, possibly bindings with, eg, somme NSArray in Applet. Connect the "nibView" of file's owner to what you want (either a window or any NSView, eg a box).
That's all.


to use it
NSView *v=[anApplet getView];
if (v) {
[v setFrameOrigin:loc]; // loc is NSPoint where i want to draw my view
[boardview addSubview:v];
[v setHidden:NO]; // probably not need
[boardview setNeedsDisplay:YES];
}


(of course, you get each time new instances of objects archived in the nib file)

Open question : how do I release all objects loaded (here I can easily send a release to nibView, but not to e.g. controllers present in nib. Should i add outlet to all objects in nib file so i can release them ? (not yet tryed)

I suppose I have to do everything programmatically.

You're possibly not going in the right direction. Assuming you're familiar with Model-view-controller paradigm : you only need to write code in the "model" part (at least for simple applications). Welcome in XXI th century :)
(I see very few case where you really want to load nib programatically : either you want to make several instances of a window (my case), or possibly nib is stored in a plug-in that is loaded programatically...)


Regards
Daniel
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Getting a NSView pointer from a nib
      • From: Ondra Cada <email@hidden>
References: 
 >Getting a NSView pointer from a nib (From: Milen Dzhumerov <email@hidden>)

  • Prev by Date: Re: comboboxcell datasource problem
  • Next by Date: Re: comboboxcell datasource problem
  • Previous by thread: Re: Getting a NSView pointer from a nib
  • Next by thread: Re: Getting a NSView pointer from a nib
  • Index(es):
    • Date
    • Thread