Re: Newb: Targeting an instance of a class instantiated by a NIB file
Re: Newb: Targeting an instance of a class instantiated by a NIB file
- Subject: Re: Newb: Targeting an instance of a class instantiated by a NIB file
- From: Brad Gibbs <email@hidden>
- Date: Fri, 12 Sep 2008 19:08:14 -0700
I run into a stumbling block once I get to the first
NSWindowController, whether I'm trying your method, or trying to use
the Responder Chain, as Jamie suggested. I get what you're suggesting
and I tried going that route before making the original post, but I
don't know how to go from the NSWindowController that contains the
buttons over to the other NSWindowController that contains the view
switching code. A button on the MainWindowController launches the
MainMenuWindowController, but the two don't have any kind of direct
relationship through a hierarchy. So, when I use
[self.view.window.windowController], i can get to the controller of
the view containing the buttons, but I don't where to go from there,
given that there is no direct relationship between that window
controller and anything on the main window or its controller.
I tried to make the MainMenuWindow a child of the MainWindow, and
while that did allow the buttons to control the view switching, it
caused all sorts of other problems, since calls such as
[self.view.window close] that were intended to close just the
MainMenuWindow began closing both windows.
I tried:
[self.view.window.parentWindow.windowController removeChildWindow:
[self.view.window]];
[self.view.window close];
but XCode spit out an error -- I suppose XCode sees this as self-
referential?
So, the problem remains...
As far as the responder chain goes, the message goes up from the
NSViewController containing the buttons through its window, to that
window's controller and then to NSApp. I don't know how to cause it
to jump to the main window's window controller.
On Sep 12, 2008, at 5:15 PM, Jonathan Hess wrote:
On Sep 12, 2008, at 3:07 PM, Brad Gibbs wrote:
Thanks for the help. I'm trying to understand your code suggestion
below. My app isn't document-based, so I don't think I have a
document to refer to... Can I just leave that part out, or will
things work differently in a non-document-based app?
Ah. That was just an example that I figured would be close enough to
lead you to the solution that was right for your App. You'll need to
replace the "windowController] document] mainWindowController]" part
with whatever series of messages allow you to get a reference back
to your main view controller.
As for deciding what the right chain of messages is ... At some
point your view controller probably knows about some object, that
knows some other object, that knows another object ... that finally
knows your main window controller. If there isn't some chain of
relationships you can follow through your objects to get from your
view controller to your window controller, you could introduce one.
Once you've done that, you'll use that path to get from self, the
view controller, to your main window controller.
I've also re CC'd the list because I think this thread will be
interesting to new cocoa developers searching through the list
history for help -
Jon Hess
The bit I wrote about the responder chain was in response to Jamie
Hardt's suggestion that I use the responder chain (which I couldn't
get to work, either).
On Sep 12, 2008, at 2:43 PM, Jonathan Hess wrote:
On Sep 12, 2008, at 2:25 PM, Brad Gibbs wrote:
If I'm reading your mail correctly, I've tried that without
success.
I have a MainWindowController controlling MainWindow. On
MainWindow.xib is a button which launches another window
(MainMenu,xib) with a window controller
(MainMenuWindowController.m). A couple of NSViewControllers down
is a view with the buttons in question.
I've tried creating an instance of MainWindowController in that
view's NSViewController (declaring MainWindowController
*iMainWindowController in the interface, using @property and
@synthesize and then calling iMainWindowController =
[[MainWindowController alloc] init] in the NSViewController's
implementation section).
The problem is that this creates a new 'MainWindowController'. The
object you get in response to [[MainWindowController alloc] init]
is unrelated to the one previously created.
I can tell the method is being called by an NSLog statement
posted to the Console, but the view doesn't swap in. I'm
assuming this is because I've programmatically created another
instance of the MainWindowController class in the
NSViewController and I'm targeting that instance with the
buttons, rather than the instance created at launch, which is
controlling the MainWindow.
Exactly.
Calling a method on the MainWindowController instance created by
the NSViewController would still cause the log file to print the
message without swapping in the view on the instance of the Main
Window created when I launch the app. So, I'm left to assume
that I need to target the instance created at launch.
Yep.
According to the Event Handling Guide:
If an NSWindowController object is managing the window, it
becomes the final next responder.
Is this going to be a problem, since the NSViewController with
the button code is underneath its own NSWindowController? Would
the responder chain just stop there and so that messages wouldn't
make their way over and up to the MainWindowController?
The method I suggested is independent of the responder chain. So
this won't come into play. If you follow the other suggestion,
this will be relevant.
If you choose to peruse my suggestion further, you'll need to make
a way to find the original MainWindowController from the code
inside of your view controller. If your document is holding onto a
reference to the main window controller, perhaps your view
controller can do something like '[[[[self window]
windowController] document] mainWindowController]'.
Good Luck -
Jon Hess
Thanks again.
Brad
On Sep 12, 2008, at 1:38 PM, Jonathan Hess wrote:
Hey Brad -
So it sounds like you have two controllers, A, and B, and they
each have their own NIB. Sound like you're on the right track.
Now you want to have an action in B's NIB affect controller A.
Does controller B have an instance variable, or other mechanism,
for referencing controller A? If so, you could put an action
method on controller B and have that be the target of your
button in NIB B. The implementation of that action can then call
a method on controller A.
Jon Hess
On Sep 12, 2008, at 12:17 PM, Brad Gibbs wrote:
I'm working on an application with a single main window and a
number of views and view controllers. I have a navigation
window that pops up and allows users to set preferences and
also switch views in the main portion of the app's main window.
I want to write the code to switch views in the
MainWindowController.m, since it controls the window that
contains the views that will be switched. But, the buttons
that control the view switching are located on a panel being
controlled by a different view controller.
The main window and its window controller are instantiated when
the program is launched. Creating another instance in the view
controller that contains the buttons (either by adding a
MainWindowController object in IB or by creating another
instance in the view controller code) doesn't target the
existing main window, and, therefore, doesn't cause views to be
switched in the main window that is instantiated on launch.
So, how can I target the instance of the main window controller
that was instantiated on launch from the NSViewController
controlling the view with the view switching buttons, in order
to switch views? Or should I be doing something else entirely?
Thanks.
Brad
_______________________________________________
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:
@apple.com
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