Re: Is it possible to pass an object to a NIB

2009-12-06 Thread DeNigris Sean
I tightened up the code a bit:
#Obj-C version: - (id)initWithContentsOfURL:(NSURL *)nibFileURL
nib_file = NSNib.alloc.initWithContentsOfURL NSURL::fileURLWithPath(@@NibPath)

#Obj-C version: - 
(BOOL)instantiateNibWithOwner:(id)ownertopLevelObjects:(NSArray 
**)topLevelObjects
#The Ruby bridge puts the top level objects in the second return value because 
the 2nd param takes a pointer
# and Ruby does not have pointers
result, top_level_objects = nib_file.instantiateNibWithOwner_topLevelObjects 
NSApplication.sharedApplication

view = top_level_objects.find { |obj| obj.class == object_type }

- Sean___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is it possible to pass an object to a NIB

2009-12-05 Thread DeNigris Sean
 Really, the external name table is for referring to objects in nibs, rather 
 than pushing objects into nibs.
Thanks.  That's what I wanted to know - I thought the docs suggested maybe you 
could pass objects in (other than owner)

 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.  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?

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 }

Sean DeNigris
s...@clipperadams.com

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is it possible to pass an object to a NIB

2009-12-05 Thread Chris Hanson
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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Is it possible to pass an object to a NIB

2009-12-04 Thread DeNigris Sean
Hi list!

I'm writing a RubyCocoa app, but my question is on the Cocoa API...

I'm trying to unit test a view class.  As it is very thin (just delegates all 
work to the controller), all I want to check is that my connections (e.g. 
outlets and actions) are hooked up correctly.

I've been trying to:
1. Create an instance of my class
2. use NSBundle::loadNibFile: externalNameTable: withZone: to load the nib
3. check the connections

The docs for loadNibFile 
(http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSBundle_AppKitAdditions/Reference/Reference.html)
 seem to suggest that you can pass objects in:
A name table whose keys identify objects associated with your program or the 
nib file. The newly unarchived objects from the nib file use this table to 
connect to objects in your program.

But I can't get it to work after hours!

This code is in ruby, but it's just bridged to the corresponding Cocoa calls:
view = MyView.new # subclass of NSWindowController
NSApplication.sharedApplication
top_level = [] # I've tried passing everything I could think of here:
- view # the object instance
- My View = view # NSDictionary with key 
object name in IB and value object instance
- MyView = view # NSDictionary with class name 
/ object entry

# I Also tried passing the object, and object-containing dictionaries 
below
# e.g. dictionaryWithObjects_forKeys [NSApp, top_level, view], 
[NSNibOwner, NSNibTopLevelObjects, My View]
context = NSDictionary::dictionaryWithObjects_forKeys [NSApp, 
top_level], [NSNibOwner, NSNibTopLevelObjects]

NibPath = 
.../RandomAppRuby.app/Contents/Resources/English.lproj/MainMenu.nib

OSX::NSBundle::loadNibFile_externalNameTable_withZone NibPath, context, 
NSApp.zone

Thanks in advance for any guidance!

Sean DeNigris
s...@clipperadams.com



___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Is it possible to pass an object to a NIB

2009-12-04 Thread Chris Hanson
On Dec 4, 2009, at 7:47 PM, DeNigris Sean wrote:

 I'm writing a RubyCocoa app, but my question is on the Cocoa API...
 
 I'm trying to unit test a view class.  As it is very thin (just delegates all 
 work to the controller), all I want to check is that my connections (e.g. 
 outlets and actions) are hooked up correctly.
 
 I've been trying to:
 1. Create an instance of my class
 2. use NSBundle::loadNibFile: externalNameTable: withZone: to load the nib
 3. check the connections

Really, the external name table is for referring to objects in nibs, rather 
than pushing objects into nibs.

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.

All you should need to do is instantiate your window controller, invoke its 
-window method to force it to load its associated nib file, and then check that 
its outlets are wired up as you expect.

  — Chris

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com