Lefora Free Forum
login join
Loading
1412 views

Access AppController instance attribute from another class.

Page 1
1–16
rookie - member
5 posts

Good evening,

I've a cocoa application with a AppController class which control my application (awakeFromNib, button action, that kind of stuff) with a NSArrayController. I create also an other class (a subclass of NSViewController but it doesn't matter) that is used in my nib.

I would like to access to the array controller of the AppController class from my NSViewController subclass (no matter why), I've the @property/synthesize declaration for the array controller in my AppController class, but I just don't know how to obtain a pointer to my AppController instance.

I hope I'm understandable, sorry for my way of talking, I'm French.

Good night,
Valleix007

?
63 posts

Salut Valleix. Je suis français aussi! ;)

Je crois bien que tu peux avoir accès comme ceci:
// fichier H
#import <Foundation/Foundation.h>
@class AppController;
@interface ValleixArrayController: NSArrayController
{
   AppController *appController;
   // le reste ici
}
-(IBAction)faireQuelqueChose:(id)sender;
// et ici
@end

// fichier M
#import "AppController.h"
#import "ValleixArrayController.h"
@implementation ValleixArrayController: NSArrayController
-(id)init {
    [super init];
    appController = [[AppController alloc] init];
    return self;
}
-(void)dealloc {
    [appController release];
    [super dealloc];
}
-(IBAction)faireQuelqueChose:(id)sender {
    id property = appController.property;
    // je sais plus!
}
@end

Hi Valleix. I'm French too!
I think you could access it like this:
// H file
#import <Foundation/Foundation.h>
@class AppController;
@interface ValleixArrayController: NSArrayController
{
   AppController *appController;
   // the rest here
}
-(IBAction)doSomethang:(id)sender;
// and here
@end
// M file
#import "AppController.h"
#import "ValleixArrayController.h"
@implementation ValleixArrayController: NSArrayController
-(id)init {
    [super init];
    appController = [[AppController alloc] init];
    return self;
}
-(void)dealloc {
    [appController release];
    [super dealloc];
}
-(IBAction)faireQuelqueChose:(id)sender {
    id property = appController.property;
    // idk what else you want!
}
@end
Laisse-moi savoir si t'as d'autres questions. // Lemme know if you got other questions.

__________________
kompilesoft

come and see my website! it's real cool reviews
?
288 posts

Pas de problem!  I suis ecossais, mon francais est terrible, mais votre anglais est mielleur.  Notre ami kompilesoft est correcte.  C'est quelquechose comme ca.


Something very like this came up yesterday and I wrote a little program TwoDialogs to show how to use C extern objects - so there's a extern Dialog1* dialog and extern Dialog2* dialog2.  Each dialog inits its corresponding extern from it's awakeFromNib.  Simple?, Mais oui.

Just for fun, I've modified that this evening.  There's a global NSApp that points to your application (not your application controller, your application).  The program has an application delegate, and I added a property stringValue to that.  So anywhere in the program you can:

// read the app delegate property:
[[NSApp delegate]stringValue]
// set the app delegate property:
[[NSApp delegate]setStringValue:@"very very boring"];

Run the program and the first time you push "Press Me" on Dialog1, it'll bring up an alert.  Very boring.

However you can see there are a couple of ways in this program to share data.  I'm sure there are lots of other ways - however there's a couple to get you started.

Bon Chance.

__________________
rookie - member
5 posts

Thank you for your answer,
If I do a appController = [[AppController alloc] init]; I will obtain a new appController instance with new attributes' value, won't I ?
I'd like to use the attributes' values that are defined in the original instance of appController : I want to access to the original NSArray because I've a NSTableView in the nib that my NSViewController subclass owns, and this table view must present data contained in the NSArray of the AppController.

So : AppController with myArrayController and theArrayControlled. AppController owns the main nib.

NSViewControllerSubClass owns a other nib where there is a NSTableView that should display theArrayControlled.

How could access to the theArrayControlled trough the NSArrayController from my NSTableView.

The best thing I could have is an access to myArrayController from InterfaceBuilder of the other nib, without creating another instance of the NSArrayController.

ClanMills, you're French is great, thank for your code, I think it should work but I'd like to avoid to use global variable. And i would like to acess to myArrayController from IB like I do in the main nib.

rookie - member
5 posts

[NSApp delegate] works perfectly, thank you ! But if anyone knows how to do that from IB, without coding all the bindings manually, it'd be great !

Here is an example of my code :

subViewArrayController = [[NSApp delegate] appControllerArrayController];
[[[tableView tableColumns] objectAtIndex:unIndex]
bind:@"value"
toObject:subViewArrayController
withKeyPath:@"arrangedObjects.unElementDuController"
options:nil];

?
288 posts

OK.  I've made a project "OldAlliance" which has two windows (in one nib) and a sharedValue.  I've bound the property sharedString on "OldAllianceController" to the textField in both the windows.  You'll see that when you type into either window's text field, both windows update.  Good, eh?  An all done in Interface builder.http://clanmills.com/articles/cocoatutorials/OldAlliance.zip

I'm not sure that this is exactly what you want - however I think it's very close.

__________________
rookie - member
5 posts

It's close, but differant : i've two nib, and I cannot shared (trough IB) an unique NSArrayController between the two nib without outlet and [NSApp delegate].
Thank for your help !
I've solve for the moment my problem using the NSApp delegate.

?
63 posts

"Pas de problem!  I suis ecossais, mon francais est terrible, mais votre anglais est mielleur.  Notre ami kompilesoft est correcte.  C'est quelquechose comme ca."

That would be:

"Pas de problème!  Je suis écossais, mon français est terrible, mais votre anglais est meilleur.  Notre ami kompilesoft a raison.  C'est quelque chose comme ça."

If the delegate don't work right, you can come back and bug us about it.

__________________
kompilesoft

come and see my website! it's real cool reviews
?
288 posts

I have to go to work now (in California).  I'll have a look again this evening.  You've got a fix for now - so you're not stuck.

__________________
?
288 posts


I've constructed a document using a single nib which contains everything!  This nib can contain any 

number of windows  and controls which can all be bound to an array controller.  For that matter, there can be any number of array controllers.

You can download the code from:  http://clanmills.com/articles/cocoatutorials/OneNibDocument.zip

I think that in order to make IB do the binding magic, the ArrayController object and the windows will have to be in the same nib.  So, I'm wondering what's in your nibs?

Usually when the wizard creates a document application, he creates two nibs:
1) One for the application (called MainMenu.nib)2) And a second for the document (called MyDocument.nib) where you normally put your ArrayController.

MyDocument nib can contain any number of windows and controls which can be bound to the array controller.  In the code in "OldAlliance" there are two windows bound to a single property.

If you have your UI in multiple nibs, I think you can just drag the windows (and their corresponding .h and controller objects) into MyDocument.nib.

My question are:
1)  What's in your nibs?
2)  Why can't you put all your windows into the nib beside the Array Controller?"

If reading and writing in English is difficult, I'm sure Kompilesoft will be willing to translate.  He's a native French speaker and he's so fluent in English, he can even understand my Scottish accent when we talk on Skype.

__________________
?
63 posts

Well, I can't really understand you. ;) I'd like to see that OneNibDocument. Sounds quite interesting.

__________________
kompilesoft

come and see my website! it's real cool reviews
?
288 posts

Well, our friend V wanted a 100% IB method of creating the document and bindings - and that's what I've constructed.  The XCode wizard generates document apps using 2 nibs and what I've done is to create a document project (without using the wizard) that puts everything into one nib.  Problem solved.

Why do you not understand?

__________________
rookie - member
5 posts

I apologize for my silence but I'm sick (I've a lot of red buttons, real buttons not NSButton... Cannot dealloc them !).

So, I read your last comment and I finally put all my view in my main nib. Few code modifications later, it's working perfectly.
Because of the Aaron Hilgass's book, I've started to work with different nib, It's totally useless in my application.

Thank you very much for your explanations, I'll present you my application when it is finished (is that good English ?)

Have a good day !

?
63 posts

@clanmills: I was talking about your accent. ;)

What's your app do?

__________________
kompilesoft

come and see my website! it's real cool reviews
?
288 posts


Mon dieu, ce sont de bonnes nouvelles. Je voudrais voir votre application. Comme toujours, votre anglais est meilleur que mon français. Peut-être mon cacao est supérieure.

This is good news and I look forward to seeing your application.  Congratulations.  We've both learnt something here.  I'd never thought about creating a document application without using the wizard.  So we've both learnt things here.  Good.

I hope you are well soon.

__________________
?
288 posts

Hey K.  Yes, I know you don't always understand my accent.  When I went to work in Finland, the guys there were didn't know what I was saying when I started.  7 years later, they all spoke much better English with Scottish accents.  Beware!

__________________
Page 1
1–16

Locked Topic


You must be a member to post in this forum

Join Now!