Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
667 views
in Technique[技术] by (71.8m points)

objective c - Cocoa Binding Mutable Dictionary with Search Field

In my header file, I have a property declared

@property (strong) NSMutableDictionary *myContents;

In my implementation, I have assigned the contents of a plist (which are dictonaries) to the NSMutableDictionary.

In my window, I have a NSSearchField that I would like to bind with the Dictionary and that is the problem. I have a few questions and still don't understand after reasearching.

(1)Which should I use? NSObjectController, NSDictonaryController, or NSTreeController.

(2)How should I bind the controller to the dictionary? Is there a "key" and a "value" that I can bind or do I have to use properties declared in another class and add it to the dictionary?

(3)Can I bind that into a NSTableView with a "Key" on one side and a "value" on another side?

Thank you!

UPDATE My plist is like this Dictonary(root)>Dictionary>Strings. The way I set up my dictionary is like this:

NSString *myListPath = [[NSBundle mainBundle] pathForResource:@"Collection" ofType:@"plist"];
self.Dictionary1 = [[NSMutableDictionary alloc]initWithContentsOfFile:myListPath];

And I access one of them by :

    NSString *initialString = [[_Dictionary1 valueForKey:@"Book1"]objectForKey:@"1"];

Book1 is a Dictionary(2nd one). "1" is the string and is a chapter of Book1. How do I split this up then?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

(1)Which should I use? NSObjectController, NSDictonaryController, or NSTreeController.

Use NSDictionaryController.

(2)How should I bind the controller to the dictionary? Is there a "key" and a "value" that I can bind or do I have to use properties declared in another class and add it to the dictionary?

Step 1 : Create @property(strong)NSMutableDictionary *dict; in .h file

Step 2 : Create a NSDictionaryController.

Step 3 : Check prepares content.

Step 4 : Set as :

enter image description here

Step 5 : Create a NSTableView.

Step 6 : For first column do as:

enter image description here

Step 7 : similarly for second column put value in Model Key Path.

Find the running sample here.

(3)Can I bind that into a NSTableView with a "Key" on one side and a "value" on another side?

Already done.

Binding NSDictionaryController with NSTableView by NSSearchField

Step 1 : Draw a NSSearchField.

Step 2 : Bind it to NSDicitonaryController in Predicate option.

Step 3 : In predicate format put key contains $value

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...