2011-04-22 1 views
0

NSTreeControllerNSArrayController이며 엔터티에 바인딩되어 있고 트리는 NSOutlineView에 바인딩되어 있습니다. 이제 "추가"버튼을 클릭하면 treeController에 새 엔티티를 추가하고 엔티티를 선택한 다음 편집을 위해 강조 표시하고 싶습니다. [arrayController add]을 호출하면 삽입이 비동기 적이며 개요보기가 새 행을 자동으로 선택하지 않으므로 새 객체가 무엇인지 알 수 없습니다. 그래서 새로운 엔티티를 프로그램 적으로 삽입하는 것으로 남았습니다. 따라서 addButtonoutlineViewController (아래 참조)의 createNewGroup으로 전화합니다.삽입 된 항목을 자동으로 선택하는 방법 및 NSOutlineView 및 코어 데이터

다시 말하면 새로운 엔티티를 삽입하는 것이 동기 프로세스가 아닌 것 같습니다. currentObject = [NSEntityDescription... 다음 줄의 NSOutlineView에서 찾을 수 없습니다. 그리고 데이터를 다시로드 한 후에 시도했습니다. 그래서 나는 어레이 컨트롤러의 값 변화를 관찰하면서 떠났다. 이런 종류의 작품은 대개는 그렇지만 때로는 그렇지 않습니다. 이런 것이 올바른 접근 방식입니까?

- (void) createNewGroup:(id)sender { 
    NSInteger row = [myOutlineView selectedRow]; 
    if(row == -1) { 
     [groupsController addObserver:self 
          forKeyPath:IR_GROUPS_KEYPATH 
           options:NSKeyValueObservingOptionInitial 
           context:IR_GROUPS_CONTEXT]; 
     currentObject = [NSEntityDescription insertNewObjectForEntityForName:@"Group" 
                 inManagedObjectContext:appDelegate.managedObjectContext]; 
     return; 
    } 
    if([myOutlineView levelForRow:row] != 0) return; 
    [subGroupsController addObserver:self 
          forKeyPath:IR_GROUPS_KEYPATH 
          options:NSKeyValueObservingOptionInitial 
          context:IR_SUBGROUPS_CONTEXT]; 
    NSManagedObject *parent = [[myOutlineView itemAtRow:row] representedObject]; 
    currentObject = [NSEntityDescription insertNewObjectForEntityForName:@"Group" 
                  inManagedObjectContext:appDelegate.managedObjectContext]; 
    [currentObject setValue:parent forKey:@"parent"]; 
} 

- (void) observeValueForKeyPath:(NSString *)keyPath 
         ofObject:(id)object 
         change:(NSDictionary *)change 
         context:(void *)context { 

    if([keyPath isEqualToString:IR_GROUPS_KEYPATH]) { 
     if(currentObject == nil) return; 
     [myOutlineView noteNumberOfRowsChanged]; 
     NSString *ctx = (NSString *) context; 
     if([ctx isEqualToString:IR_GROUPS_CONTEXT]) { 

      NSInteger length = [myOutlineView numberOfRows]; 
      NSInteger index; 
      for(index = 0; index < length; index++) { 
       id item = [myOutlineView itemAtRow:index]; 
       if(currentObject == [item representedObject]) { 
        // We found the new object: 
        NSIndexSet *indices = [NSIndexSet indexSetWithIndex:index]; 
        [myOutlineView selectRowIndexes:indices byExtendingSelection:NO]; 
        [myOutlineView editColumn:0 row:index withEvent:nil select:YES]; 
        currentObject = nil; 
        return; 
       } 
      } 
      //[groupsController removeObserver:self forKeyPath:nil]; 

     } else if([ctx isEqualToString:IR_SUBGROUPS_CONTEXT]) { 
      NSTreeNode *parent = [myOutlineView itemAtRow:[myOutlineView selectedRow]]; 
      [myOutlineView expandItem:parent]; 
      NSInteger length = [myOutlineView numberOfRows]; 
      NSInteger index; 
      for(index = 0; index < length; index++) { 
       id item = [myOutlineView itemAtRow:index]; 
       if(currentObject == [item representedObject]) { 
        NSIndexSet *indices = [NSIndexSet indexSetWithIndex:index]; 
        [myOutlineView selectRowIndexes:indices byExtendingSelection:NO]; 
        [myOutlineView editColumn:0 row:index withEvent:nil select:YES]; 
        currentObject = nil; 
        return; 
       } 
      } 
     } 
    } 
} 
+1

질문에 답변으로 표시해주십시오. – JJD

답변

1

NSTreeController의 하위 클래스를 사용하여 outlineView의 콘텐츠를 제공하는 경우 매우 간단합니다. 코드 나 인터페이스 빌더에서 단추를 만들고 대상을 insert:에 바인드하여 요소를 추가하거나 요소 remove:을 삭제하도록 설정합니다. 코드에서 다음과 같이 보일 것입니다 :

[aButton bind:NSTargetBinding 
    toObject:aController 
    withKeyPath:keyPathToTreeController 
    options:[NSMutableDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithBool:YES], NSConditionallySetsEnabledBindingOption, 
          @"insert:", NSSelectorNameBindingOption, 
          nil]]; 

새 개체를 선택하는 것은 treeController에 의해 처리됩니다. 코드에서 다시 :

[aTreeController setSelectsInsertedObjects:YES]; 

IB에서 확인해야하는 체크 박스입니다. 아, addChild:도 있습니다. 바인딩이 그들의 마술을하도록하십시오.

+0

고마워, 나는 그것을 시도 할 것이다! – izk

+0

행운을 비네. 마음, 나는 NSOutlineView (또는 NSTreeController)를 사용하는 것이 쉽다는 말은하지 않았습니다. (Apple 개발자 기술 지원 - 아직 최신 티켓을 알아 냈습니까?)하지만 그 부분은 진실입니다. –

관련 문제