Re: Is it possible to pass an object to a NIB
Re: Is it possible to pass an object to a NIB
- Subject: Re: Is it possible to pass an object to a NIB
- From: Chris Hanson <email@hidden>
- Date: Sat, 05 Dec 2009 15:54:41 -0800
On Dec 5, 2009, at 3:46 PM, DeNigris Sean wrote:
>> Furthermore, from your further description it sounds like what you’re referring to as a “view” is actually a subclass of NSWindowController; it knows how to load a nib file already, so you should just leverage that rather than try to do it all yourself by hand.
>
> Thanks again. It's an "ultra-thin gui" a-la Dave Astels' TDD: A Practical Guide (originally Mike Feathers' Humble dialog). There is another controller class that handles all the logic. This class just delegates the actions and outlets from Cocoa, so from my app's perspective, it's a view, but from cocoa's perspective, it's a controller.
Many such techniques were created for frameworks that don’t have the separation of concerns that Cocoa does. In Cocoa, you generally don’t need to follow patterns like Feathers’ “Humble Dialog Box” because you aren’t constrained to testing by pushing events through the run loop.
Rather than try to follow patterns not designed for Cocoa, I would strongly encourage you to follow Cocoa’s own patterns when writing Cocoa code, whether you’re doing so in Objective-C or some other language like Ruby.
> It think it should be just an NSObject (that's what I've seen in all the books and there's only one window in my app). I made it an NSWindowController in the process of trying to get it to load the nib (which is only for testing) and forgot to set it back. Is there a reason to keep it an NSWindowController?
NSWindowController already knows how to load nibs and act as File’s Owner. It and NSViewController also have some subtle support for features like breaking the circular retain that results when you bind through File’s Owner. In general, I would *not* use subclasses of anything *but* NSWindowController and NSViewController as the File’s Owner for a nib without a very good reason.
> I ended up with the following that loaded the nib successfully in the test:
> NSApplication.sharedApplication
> top_level = []
> context = NSDictionary::dictionaryWithObjects_forKeys [NSApp, top_level], [NSNibOwner, NSNibTopLevelObjects]
>
> @@NibPath = "/path/to/MainMenu.nib"
> OSX::NSBundle::loadNibFile_externalNameTable_withZone @@NibPath, context, NSApp.zone
>
> objects = context['NSTopLevelObjects']
> view = objects.find { |obj| obj.class == object_type }
I think you’re doing too much work here. You should use an IBOutlet in File’s Owner to refer to your view, rather than look through the array of top-level objects trying to find it. The latter is simply not how things are typically done in Cocoa.
— Chris
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden