0

(このサンプル プロジェクトはこちらhttps://github.com/danieljfarrell/BindingToPopUpButtons )

私はちょうどバインドに入っていますが、AppDelegate (モデル) でコンテンツ配列を管理している NSArrayController にバインドされた NSPopUpButton があり、すべてうまく機能しています! ただし、メソッドのコンテンツ配列に追加された静的オブジェクトに対してのみうまく機能します-init。コンテンツ配列を変更するときに問題が発生します (挿入、追加など...)。

-init で読み込まれた曲

// AppDelegate.m
- (id)init
{
    self = [super init];
    if (self) {
        _songs = [[NSMutableArray alloc] init];
        NSMutableDictionary *song1 = [NSMutableDictionary dictionaryWithDictionary:@{@"title" : @"Back in the USSR"}];
        NSMutableDictionary *song2 = [NSMutableDictionary dictionaryWithDictionary:@{@"title" : @"Yellow Submarine"}];

        [_songs addObjectsFromArray:@[song1, song2]];
    }
    return self;
}

問題。新しい曲を挿入してコンテンツ配列を-mutableArrayValueForKey:変更すると、NSPopUpButton は-description配列要素の値ではなく配列を表示し、配列が重複しているように見えますか? モデルが単なる NSMutableDictionary であるこの単純なケースでは、KVO 準拠の方法でコンテンツ配列を適切に変更するにはどうすればよいですか?

データ ソースの変更

// AppDelegate action method from button click
- (IBAction)addNewSong:(id)sender {

    // Grab the new song title from a text field
    NSString *newSong = self.songTextField.stringValue;
    // Grab the insert index from a text field.
    NSInteger index = self.indexTextField.integerValue;

    /* Here I want the array controller to
       create a new NSMutableDictionary
       and set the title key with the new song. */
    [[self.songs mutableArrayValueForKey:@"title"] insertObject:newSong atIndex:index];

    /* I also tried adding a dictionary but ran into a similar problem...*/
    // [[self.songs mutableArrayValueForKey:@"title"] insertObject:[@{@"title" : newSong} mutableCopy] atIndex:index]; 
}

NSPopUpButton のバインディングは標準です。

  • コンテンツ

    • バインド先:配列コントローラー
    • コントローラーキー: arrangedObjects
  • コンテンツ値

    • バインド先:配列コントローラー
    • コントローラーキー: arrangedObjects
    • モデル キー パス:タイトル (arrangedObjects 配列に含まれる NSDictionary アイテムのキー)。
4

1 に答える 1

2

Volker は、NSArrayController のアウトレットを作成し、このようなことを行うことを意味すると思います

NSMutableDictionary *song = [NSMutableDictionary dictionaryWithDictionary:@{@"title": newSong}];

[[self myArrayController] addObject:song];
于 2014-02-16T01:45:37.253 に答える