1

NSPopUpButton に NSString の配列を設定したいと考えています。また、選択した項目を NSPopUpButton に設定し、選択した値を取得できるようにしたいと考えています。バインディングを使用してこれを行う方法はありますか? これは、配列コントローラーの内容のみがarragedObjectsにバインドされているものです。

@interface AppDelegate : NSObject <NSApplicationDelegate>
{
    NSMutableArray *myArray;
    IBOutlet NSPopUpButton *myPopUpButton;
    IBOutlet NSArrayController *processArrayController;   
}

@property (assign) IBOutlet NSWindow *window;
@end

@implementation AppDelegate
@synthesize window = _window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
     NSString *firstObject = @"Lustre";
     NSString *secondObject = @"TwoTone Laser";
     NSString *thirdObject = @"Laser Mark";
     NSString *forthObject = @"Double Lustre";
     NSString *fifthObject = @"CG Ink";

    // Create the array
     myArray = [[NSMutableArray alloc] initWithObjects:firstObject, secondObject,  
     thirdObject, forthObject, fifthObject, nil];

    // Sort the array
    [myArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

    // Set contents of the array controller that is bound to the popup button
    [processArrayController setContent:myArray];

    // Set a selection to an item of the popup button
    [myPopUpButton  selectItemWithTitle: firstObject];  
}
@end
4

1 に答える 1

0

アプリケーションコントローラでivarを設定して、選択を保持します。

@property (copy) NSString *selection;

そしてもちろん、実装ファイルでそれを合成します。

NSPopUpButtonインスタンスに次のバインディングを作成します。

コンテンツ:

バインド先:アレイコントローラー(アレイコントローラーに別の名前を付けた場合を除く)

コントローラキー:arrangedObjects

コンテンツ値:

バインド先:アレイコントローラー(アレイコントローラーに別の名前を付けた場合を除く)

コントローラキー:arrangedObjects

モデルキーパス:(文字列の場合、私は常に「説明」を使用しました)

選択したオブジェクト:

バインド先:アプリケーションデリゲート(アプリケーションデリゲートに別の名前を付けた場合を除く)

モデルキーパス:self.selection

最後に、ポップアップボタンがにバインドされているのでselection、これが最初の選択を設定する方法です。

self.selection = firstObject;

あなたの努力であなたに幸運を。

于 2012-11-29T19:28:23.797 に答える