Re: Accessing Variables from Multiple NIBs

2010-01-17 Thread Quincey Morris
On Jan 16, 2010, at 22:08, Jon Buys wrote:

 Well, before this goes any further, I'm going to go ahead and answer my own 
 question here.
 
 The problem is that in the code below, I'm actually instantiating two 
 AppController objects, one in each NIB.  So, one AppController doesn't have 
 any idea about the other AppController, and can't get to it's string.
 
 The solution I've come up with is to replace my window controller class with 
 a simple call to NSBundle to load the NIB, setting AppController as the owner 
 of the second NIB.  Then, in IB, I set the identity of File's Owner to 
 AppController, delete the NSObject, and bind the button to the IBAction in 
 the File's Owner. 
 
 It's great to solve my own problems, I just wish I'd do it before sending out 
 to Cocoa-Dev for help!

Well, your solution may be functional, but there's an easier way -- one that 
leverages standard behavior.

There are two parts to this. The first is to instantiate your singleton 
AppController object in just one nib file. A good place for this is MainMenu, 
since you have only one of those. The trick is to connect the Application 
pseudo-object's delegate outlet to your AppController object.

Then in *any* nib file whose contents need to bind to the AppController object, 
you can refer to Application.delegate (since the Application pseudo-object is 
available in any nib file, and it always refers to the same object, so its 
delegate property always refers to your AppController singleton.)

That takes care of bindings, typically. However, although you said bind 
above, you likely meant connect, since bindings are made to a property, not 
to an IBAction method.

For target/action connections, you connect the selector outlet (in the Sent 
Actions section of the Connections tab) of your buttons or other controls to 
First Responder in their own nib file, and choose the appropriate AppController 
selector from the list. Because your AppController is the application's 
delegate it is therefore in the responder chain, and -- assuming nothing else 
implements the same selector -- the action will get routed to your 
AppController without doing anything clever to the nib file's owner.

Note that the list of selectors available for the First Responder pseudo-object 
is an amalgamation of all the IBAction methods known in your Xcode project's 
header files. So long as you put your AppController IBAction prototypes in your 
AppController.h file, IB will synchronize with Xcode and know they exist.

Note also that changing a window nib's File Owner from a NSWindowController (or 
a NSDocument, which has similar behavior wrt the nib file) to some other class 
is not a good idea. NSWindowController, NSDocument and NSViewController all 
have built-in functionality to manage the ownership of top level nib objects. 
If your replacement doesn't have similar functionality, you will either leak 
nib objects or crash with memory management errors.


___

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


Accessing Variables from Multiple NIBs

2010-01-16 Thread Jonathan Buys
Hello,

I have an AppController that looks like this:

AppController.h:
#import Cocoa/Cocoa.h
@class PostController;

@interface AppController : NSObject 
{
PostController *postController;
NSString *theString;
}

- (IBAction)setString:(id)sender;
- (IBAction)viewString:(id)sender;

- (void)processString;

@end

AppController.m:

#import AppController.h
#import PostController.h

@implementation AppController


- (IBAction)setString:(id)sender
{
// Is postController nil?
if (!postController) {
//  NSLog(@Post Controller was nil.);
postController = [[PostController alloc] init];
}

//  NSLog(@Showing %@, postController);
[postController showWindow:self];

theString = @The String;

NSLog(@Current Selected Site: %@, theString); 
[self processString];
}

- (IBAction)viewString:(id)sender
{
NSLog(@viewString Method: %@, theString);
[self processString];
}

- (void)processString
{
NSLog(@processString Method: %@, theString);
}


@end

I have two XIB files, MainMenu and NewPost.  I've dragged an NSObject into each 
XIB, and set it's class to AppController.  In MainMenu, I have two buttons, one 
bound to the setString action, and one set to the processString action.  In 
MainMenu XIB, I can see theString in the NSLog output.  In the NewPost XIB, I 
have one button bound to the processString method, but from this XIB, every 
time I run the processString method, theString is  null.  

How can I define a variable (like NSString) in one XIB, and be able to access 
it from methods triggered from another XIB?

I've got a simple Xcode project that shows this problem, if it would help with 
explaining it.

Thanks,

Jon

PS.  Here is the PostController too, just in case:

#import Cocoa/Cocoa.h


@interface PostController : NSWindowController 
{
}

@end


#import PostController.h
#import AppController.h


@implementation PostController

- (id)init
{
if (![super initWithWindowNibName:@NewPost])
return nil;

return self;
}


- (void)windowDidLoad
{
NSLog(@Nib file is loaded!);
}

@end

___

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: Accessing Variables from Multiple NIBs

2010-01-16 Thread Jon Buys
Well, before this goes any further, I'm going to go ahead and answer my own 
question here.

The problem is that in the code below, I'm actually instantiating two 
AppController objects, one in each NIB.  So, one AppController doesn't have any 
idea about the other AppController, and can't get to it's string.

The solution I've come up with is to replace my window controller class with a 
simple call to NSBundle to load the NIB, setting AppController as the owner of 
the second NIB.  Then, in IB, I set the identity of File's Owner to 
AppController, delete the NSObject, and bind the button to the IBAction in the 
File's Owner.  

It's great to solve my own problems, I just wish I'd do it before sending out 
to Cocoa-Dev for help!

-- Jon


On Jan 16, 2010, at 6:45 PM, Jonathan Buys wrote:

 Hello,
 
 I have an AppController that looks like this:
 
 AppController.h:
 #import Cocoa/Cocoa.h
 @class PostController;
 
 @interface AppController : NSObject 
 {
   PostController *postController;
   NSString *theString;
 }
 
 - (IBAction)setString:(id)sender;
 - (IBAction)viewString:(id)sender;
 
 - (void)processString;
 
 @end
 
 AppController.m:
 
 #import AppController.h
 #import PostController.h
 
 @implementation AppController
 
 
 - (IBAction)setString:(id)sender
 {
   // Is postController nil?
   if (!postController) {
   //  NSLog(@Post Controller was nil.);
   postController = [[PostController alloc] init];
   }
   
   //  NSLog(@Showing %@, postController);
   [postController showWindow:self];
   
   theString = @The String;
   
   NSLog(@Current Selected Site: %@, theString); 
   [self processString];
 }
 
 - (IBAction)viewString:(id)sender
 {
   NSLog(@viewString Method: %@, theString);
   [self processString];
 }
 
 - (void)processString
 {
   NSLog(@processString Method: %@, theString);
 }
 
 
 @end
 
 I have two XIB files, MainMenu and NewPost.  I've dragged an NSObject into 
 each XIB, and set it's class to AppController.  In MainMenu, I have two 
 buttons, one bound to the setString action, and one set to the processString 
 action.  In MainMenu XIB, I can see theString in the NSLog output.  In the 
 NewPost XIB, I have one button bound to the processString method, but from 
 this XIB, every time I run the processString method, theString is  null.  
 
 How can I define a variable (like NSString) in one XIB, and be able to access 
 it from methods triggered from another XIB?
 
 I've got a simple Xcode project that shows this problem, if it would help 
 with explaining it.
 
 Thanks,
 
 Jon
 
 PS.  Here is the PostController too, just in case:
 
 #import Cocoa/Cocoa.h
 
 
 @interface PostController : NSWindowController 
 {
 }
 
 @end
 
 
 #import PostController.h
 #import AppController.h
 
 
 @implementation PostController
 
 - (id)init
 {
   if (![super initWithWindowNibName:@NewPost])
   return nil;
   
   return self;
 }
 
 
 - (void)windowDidLoad
 {
   NSLog(@Nib file is loaded!);
 }
 
 @end
 
 ___
 
 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/jonbuys%40me.com
 
 This email sent to jonb...@me.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