Accessing objects on one nib file from another nib file
I have two nib files - Main.nib
and Preference.nib
In Main.nib
file I have an instance of NSView
class. Its window has a NSPopUpButton
which on clicking shows a menu. In the menu I have show Preferences menu item.
Menu item on clicking shows a preferences panel containing a color well item.
On clicking color well a color panel is displayed 开发者_JAVA百科to choose the color.
The problem is how to apply that color to main application window.
My preference panel window is in Preference.nib file.
So problem is accessing NSView
from another Nib Window.
Is there a way so that I can make connection between preference panel and my main application window(NSView
)
You're thinking about this at the wrong level. NSView
and NSWindow
are view objects in the Model-View-Controller pattern and shouldn't be used for holding application data. The color you select in your preference panel is application data and should be stored in an appropriate model object.
You could, for example, use bindings to bind the color well to the NSUserDefaultsController
object to store that data (assuming this is an application-wide setting). You didn't say exactly what the color is used for in your main window, but if the object that uses it is bindings aware, you can bind that object to the same value on the NSUserDefaultsController
and you're done.
Otherwise, you can respond to the color well's action message to store the color in an appropriate place and then send a notification using NSNotificationCenter
to tell other objects that the color has changed. You'll need to sign up any object that needs to take action when the value changes for your notification message.
Here are some resources:
- Here's an overview of the model-view-controller pattern that explains how Cocoa programs are structured
- This is a high level explanation of how Cocoa bindings work
- Here's a bunch of documents about using notifications
精彩评论