0
votes

at the risk of a public flogging, I was hoping someone could clarify my understanding of communicating between the app controller and a window controller. Here is the scenario using xcode4.

using the default AppDelegate.h and .m as the "controller" (which is also a delegate to MainMenu.xib). Have an ivar called int counter.

Created page.xib and PageController.h and .m (subclass NSWindowController). Imported it into AppDelegate.m

Used IBAction to create and view the page object. Like this: if (!page1) { PageController *page1 = [[Page

if (!page1) {
    page1 = [[PageControoer alloc] initWithWindowNibName:@"page"];
}

[page1 showWindow:sender];

So the new window pops and we can press buttons, etc. The code for the new window is all in PageController.h and .m. and things basically work.

That is the context, here is where I'm confused.

a) question: let's say I want to access that original ivar in AppDelegate.h called counter from PageController. Either retrieving or updating the variable. What approach would I take?

b) confirm: let's say I'm back in the AppDelegate and want to get access to a selector from page1. I believe I can do this as so: [page1 runaction]; or [[page1 variable] setStringValue:@"hello"]; (this complies but I'm not sure it really works because I can't get the changes into the xib view.)

ok and the stumper. Say another view was created with another view controller call it Page2Controller.h and .m. c) how should data flow between page and page2 -> via the AppDelegate or directly? what would the syntax look like to connect them together?

I've been following tutorials, but they don't really cover this back and forth messaging. Thanks for all the help!

1

1 Answers

0
votes

a) Generally, if you want to have data that is accessed by your controllers, it should be in a model which they are given access to in some way. You can access things in the app delegate using this method:

AppDelegate* appDelegate = [[NSApplication sharedApplication] delegate];
[appDelegate <some method>];

b) I don't understand what you're asking. If the app delegate has a pointer to page1, then yes, you can call it directly.

c) Again, you should have a data model of some sort. The controllers should update the data model when the user changes the view. You can use notifications, IBActions, and direct calls to do the updating, for example. You should look up the Model, View, Controller design pattern.