I have a document-based application.
In my document class,
I have a BOOL property : isActionPossible.
I also have an IBAction : - (IBAction) doTheAction:(id)sender.
Now, in MainMenu.xib, I want to connect a menu item to the action.
This is easy, when you know that the First Responder in Interface Builder "stands for" the active document of your application.
But how shall I do to bind the availability (the "enable property") of this item menu with the BOOL property isActionPossible of the active document ?
Indeed, in the list proposed, there isn't "First Responder".
Two choices:
You can implement menu validation, see Apple's docs. In short before a menu is displayed the system sends a validation request to the same object that handles the action, which includes the first responder. You simply add a method to your document class which returns a BOOL; in your case your isActionPossible property.
Use the currentDocument property of NSDocumentController which returns the document associated with the main window. You can then use bindings to bind <Document Controller>.currentDocument.isActionPossible. This approach is more general as you can bind label contents to values etc. as well as use it for menu bindings.
Related
I have a project that has a Entity called : Verse. The Verse (entity) has a Attribute called : VerseContent (String). I have it successfully linked to a Table View and I have a Search Bar on the top. The Table View is binded to VersesArrayController (NSArrayController which has it's Managed Object Context binded to my App Delegate's Managed Object Context). I don't really want to write any code and I recall that this is possible through Cocoa Bindings. How should I bind this Search Bar and my NSTableView together?
Thanks~
Bind the NSSearchField Predicate to VersesArrayController, and leave Model Key Path empty. You'll find Predicate under "Search" in the bindings tab. Then set the Predicate Format to:
verseContent contains $value
Checking Validates Immediately will give you live searching, leaving it unchecked requires you to hit Enter (or some other form of 'Submit').
How do I bind NSDictionary objects to several text fields in Interface Builder?
I would like to bind each object to a specific item in the dictionary. For example, the first text field should be bound to Actor:
You need to add a NSObjectController in Interface Builder. In the attributes inspector tab leave the standard mode "Class" and class name on "NSMutableDictionary", but switch "Prepares Content" on. In the bindings inspector bind "Content Object" to "Shared User Defaults Controller", controller key "values" and "Model Key Path" to your dictionary's key in User Defaults. Switch "Handles Content As Compound Value" on.
Now you can bind your text field (or check boxes etc). values to the ObjectController, with controller key "selection" (!) and the keys in your dictionary (as model key path).
Hope this helps. I couldn't find the answer in Apple's documentation and also not in the net, was lucky to find it out myself by trying different controller objects...
First: #keeluu's answer should be marked correct.
Doing it Programmatically
If you're doing this in code rather than through IB, there's a "gotcha" to look out for. It looks like this:
// Assume 'anObjectController' is an NSObjectController.
// Assume 'userDefaultsController' is [NSUserDefaultsController sharedUserDefaultsController]
// Assume 'someDictionary' is an NSDictionary in userDefaults.
[self.anObjectController
bind:NSContentBinding
toObject:userDefaultsController
withKeyPath:#"values.someDictionary"
options:#{NSHandlesContentAsCompoundValueBindingOption: #YES}];
If you do the above, you'll find that when you bind your UI elements to properties in someDictionary, those UI elements will correctly display the values that are IN user defaults, but when you change them (by say, clicking a bound checkbox) those values will NOT be updated in user defaults; the old values stick around.
The Reason
The binding must be to NSContentObjectBinding instead of NSContentBinding. The NSObjectController class exposes a binding named contentObject. When you bind to that, everything works properly.
This is an easy mistake to make because Xcode's code completion automatically goes for NSContentBinding and it's one of the most frequently used binding names anyway. This just cost me hours of trouble, so hopefully I saved someone else some time.
Try to add NSDictionaryController to you .xib file, then bind its Controller Content to your dictionary in User Defaults. Then you can bind text field values to Dictionary Controller (arrangedObjects key).
I never tried that but I think it must work.
Hope, it helps.
I have a tabview in which i have added 3 tabview Items.In each tab i have fields into which i want to populate data automatically. Which methods are invoked automatically when a tab is clicked.Please help me in this...
NSTabView allows you to specify an object as its delegate. This delegate object should be an instance of a class that conforms to the NSTabViewDelegate protocol. This protocol declares, amongst others methods,
-tabView:willSelectTabViewItem: to inform the delegate that a tab item is about to be selected;
-tabView:didSelectTabViewItem: to inform the delegate that a tab item has been selected.
You probably want to implement -tabView:willSelectTabViewItem: so that the fields are populated right before that tab item is selected.
After I drag a controller object to the document window, how do I give it a Boolean property?
Assuming you want the ability to expose and edit the property values of your custom controller as attributes showing up in IB's inspector, you'll need to write your own plugin which tells IB what the inspector should look like. This really can't be answered briefly.
Here's Apple's reference on IB Plugins:
http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/IBPlugInGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40004323-CH1-SW1
And here's the inspector portion:
http://developer.apple.com/mac/library/documentation/DeveloperTools/Conceptual/IBPlugInGuide/CreatingInspectors/CreatingInspectors.html#//apple_ref/doc/uid/TP40004323-CH6-SW1
If you truly just want to add a property to the controller's interface, you do this in Xcode (modifying the interface and implementation accordingly) and IB will pick up the changes automagically.
In the case of a checkbox, the checkbox itself is either checked or not. That state stores the boolean value instead of the controller (unless you wish it.)
To have the app undertake an action upon clicking it, think of it as a button instead of a data display and link it to an action method in the view controller. That's the simplest and old school way of doing it.
To use binding, you need to bind the checkbox's value attribute to a controller. Usually for binary values its an object controller. So, in IB, drag an Object controller to your nib window and bind it to your data source. Then, drag a checkmark button to the interface. In the checkmarks binding inspector. Set "value" binding to the name of the object controller, controller key to selection, keypath to the name of the data source attribute and provide a value transformer if needed.
I'm just getting started with Cocoa Bindings and while I've read through much of the documentation, I'm still struggling to implement a basic feature, making me question wether I'm doing it wrong or perhaps it's just not possible via IB.
Goal: Have a menu item called "Toggle visibility" toggle the state of a Boolean property in my application controller (AppController) called "visibility." I think this should be possible completely through IB.
(Note: CoreData is not in use here)
What I think needs to happen is I need to have an NSObjectController who's content outlet is set to my AppController class. Then I think I need to use an Action Innvocation binding. But here's where I run into trouble.
Should the Controller Key be selection? Should the Model Key Path be the name of my Boolean Property? What should Selector Name be?
Or do I need to setup a separate action method called "toggleVisibility" that I can bind to? If I did, why wouldn't I just use standard target/action associations, rather than bindings?
Any input / direction appreciated.
Goal: Have a menu item called "Toggle visibility" toggle the state of a Boolean property in my application controller (AppController) called "visibility."
Your goal tells you why you are failing.
In your description of your goal, you say that you want to set this menu item up as a command, a verb, an imperative sentence—“toggle the visibility”. But that's not the right title for the menu item.
Moreover, implementing a verb menu item with Bindings is inherently difficult because Bindings is the wrong tool for the job. Bindings is for exposing properties in your UI. It isn't for performing commands.
The correct title is supported by the correct Bindings-based solution.
The correct title of the menu item is, simply, “Visible”.
This alone should give you a hint as to how to proceed. Here's another: When the selection is visible, the menu item should have a check mark; when it is not visible, the menu item should have no mark.
You want to bind a property of the menu item to, ultimately, a Boolean property of your controller. Normally, this property of the menu item would be state, but for whatever reason, the binding for it is named value.
So, bind the value binding of the menu item to the Boolean property of your controller.