Re: Accessing Variables from Multiple NIBs
Re: Accessing Variables from Multiple NIBs
- Subject: Re: Accessing Variables from Multiple NIBs
- From: Jon Buys <email@hidden>
- Date: Sun, 17 Jan 2010 00:08:58 -0600
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 (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
_______________________________________________
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