1

NSArrayController를 지원하는 코어 데이터에 바인딩 된 NSPopupButton이 있습니다. NSPopupButton의 선택은 핵심 데이터 백업 항목에도 바인딩됩니다. 바인딩 꽤 많이는 다음과 같이"Not Selected"옵션을 추가하여 NSPopupButton 바인딩하기

-(MyWidget *)selectedWidget { 

    return [MyCunningLookupMethod widgetForID:[[self widgetID] intValue]]; 
} 

를 그리고 객체를 다른 방법으로 설정 :

 
NSArray 'A' of core data entities --> Array Controller 
Array Controller --> NSPopupButton.contentValues.arrangedObjects.name 

NSArray 'B' of core data entities --> Array Controller 
Array Controller --> NSPopupButton.selectedObject.methodOnObject.name 

selectedObject 다음과 같이 객체 객체 반환을 발견 ID를 조회 그것에 방법이있다

-(void)setSWidget:(MyWidget *)anObj { 

    [self setWidgetID:[anObj uid]]; 
} 

대개의 경우 객체는 사용 가능한 contentValues ​​목록의 객체와 일치합니다. 그러나 선택한 개체의 ID가 0 인 경우가 있는데이 경우 "선택하지 않음"이라는 사용 가능한 목록에 옵션을 표시하려고합니다.

"선택 항목 없음"과 함께 처리 할 수 ​​있지만 사용자가 다른 항목 중 하나를 선택하자 마자이 항목을 선택하면 해당 항목을 다시 선택할 수 있습니다. 선택되지 않은 자리 표시자가 목록에서 제거됩니다.

사용자에게 항목을 선택하거나 항목을 선택하지 않을 (즉, "선택하지 않음"으로 설정) 수있는 기능을 제공하고 싶습니다. 선택 사항이 변경 될 때마다 코어 데이터에서 가져온 배열을 보면서 프로그래밍 방식으로 전체 NSPopupMenu를 만드는 것이 아니라면 항상 사용자가 사용할 수있는 선택되지 않은 상태를 나타내는 목록에 메뉴 항목을 삽입하는 방법이 있습니까?

핵심 데이터 저장소에 "선택되지 않음"이라고하는 이름을 제외한 모든 0 기반 값이있는 엔티티 개체를 추가하는 것을 고려했습니다. 그러나이 작업은 올바른 방법이 아니라 실제로 데이터 관련성이없는 저장소의 빈 개체에 대한 문제를 나타냅니다.

언제나처럼, 모든 도움을 많이 주시면 감사하겠습니다.

해결이 잘 나는 정확히 Hobbes the Tige 게시 팔로우하지 못했지만, 내가 할 필요가 어디 거의 저를 넣어. IB로 바인딩하는 대신, 부모 배열이나 변경 작업을 시작하는 사용자 작업에 대한 선택 변경에 대한 객체 배열을 보낼 수있는 메서드를 만들었습니다. 그런 다음이 메서드는 NSPopupButton을 적절한 핵심 데이터 엔터티 정보로 간단히 업데이트하고 IB가 해당 개체의 selectedTag를 바인딩 할 수있게합니다.

여기

+(void)createContentsForPopup:(NSPopUpButton *)aPopupButton 
       withArray:(NSArray *)anObjectArray 
      addNotSelected:(BOOL)aFlag { 

    [aPopupButton removeAllItems]; 

    if (aFlag) {  

     if (!anObjectArray || [anObjectArray count] == 0) { 

      [aPopupButton addItemWithTitle:@"--- Not available ---"]; 
      [[aPopupButton itemAtIndex:0] setTag:0]; 

      return; 
     } 

     [aPopupButton addItemWithTitle:@"--- Not selected ---"]; 
     [[aPopupButton itemAtIndex:0] setTag:0]; 
    } 

    NSSortDescriptor * sd = [NSSortDescriptor sortDescriptorWithKey:@"name" 
                  ascending:YES]; 

    [anObjectArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sd]]; 

    for (id anObject in anObjectArray) { 

     [aPopupButton addItemWithTitle:[anObject name]]; 

     int thisItem = [aPopupButton indexOfItemWithTitle:[anObject name]]; 

     [[aPopupButton itemAtIndex:thisItem] setTag:[[anObject uid] intValue]]; 
    } 
} 

분명히 이것은 개체의 모든 어레이가 설명 필드의 name 상기 고유 식별자에 대한 uid 데 따르는 전달되는 방법에 달려있다.

작업이 완료되었습니다. :-)

답변

2

이것이 최선의 해결책은 아니지만 아이디어입니다 (아마도 조정해야 할 것입니다).

제목과 메뉴 항목을 추가

- (void)insertItemWithTitle:(NSString *)title atIndex:(NSInteger)index 

그래서,이 같은 : 인덱스 0을 퍼팅

[popupButton inserItemWithTitle:@"No selection" atIndex:0]; 

가 상단에 넣어 것입니다. 이 인덱스 0에 있다면, 당신은 그것이 알고 당신이 액션 이벤트를 얻을 때

- (void)addItemWithTitle:(NSString *)title 

그런 다음, 당신은 인덱스 0에있는 항목을 확인할 수 있습니다 : 당신이 목록의 맨 아래에 원하는 경우이 방법을 사용 "선택 없음."

-(IBAction)popupSelectionAction:(id)sender { 
    if (sender == popupButton) { 
      //Check for "No selection" 
      if ([popupButton indexOfSelectedItem] != 0) { 
       //Update core data managed object value 
      } 
    } 
} 

또는, 당신은 addItemWithTitle를 사용하는 경우이 항목의 배열에서 마지막 항목이 있는지 확인합니다.

-(IBAction)popupSelectionAction:(id)sender { 
    if (sender == popupButton) { 
      //Check for "No selection" 
      if ([popupButton indexOfSelectedItem] != ([popupButton numberOfItems]-1)) { 
       //Update core data managed object value 
      } 
    } 
} 

클래스 참조 :

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSPopUpButton_Class/Reference/Reference.html

+1

감사합니다. 올바른 방향으로 나를 놓아두면 가치가 있다고 생각합니다 .-) – Hooligancat