내가 가진 NSOutlineView에 대해 별도의 컨트롤러 클래스를 만드는 데 문제가 있습니다.NSOutlineView 용 컨트롤러 만들기
LTSidebarViewController
이라는 새 클래스를 만들고 MainMenu.xib 파일에 'workbench'에 객체를 추가하고 내 LTSidebarViewController
클래스에 링크했습니다. 또한 위임자와 데이터 소스를 MainMenu.xib의 NSOutlineView에 링크되도록 설정했습니다.
내 AppDelegate 파일에서 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
내에서이 클래스의 인스턴스를 만들고 그럴 때 App Delegate의 managedObjectContext를 전달하려고합니다. 그래서, 같은 보이는 LTSidebarViewController
에서 사용자 지정 init
방법을 만들었습니다
-(id)initWithManagedObject:(NSManagedObjectContext*)managedObject{
self = [super init];
if (self) {
self.managedObjectContext = managedObject;
NSFetchRequest *subjectsFetchReq = [[NSFetchRequest alloc]init];
[subjectsFetchReq setEntity:[NSEntityDescription entityForName:@"Subject"
inManagedObjectContext:self.managedObjectContext]];
subjectsArray = [self.managedObjectContext executeFetchRequest:subjectsFetchReq error:nil];
_topLevelItems = [NSArray arrayWithObjects:@"SUBJECTS", nil];
// The data is stored in a dictionary
_childrenDictionary = [NSMutableDictionary new];
[_childrenDictionary setObject:subjectsArray forKey:@"SUBJECTS"];
// The basic recipe for a sidebar
[_sidebarOutlineView sizeLastColumnToFit];
[_sidebarOutlineView reloadData];
[_sidebarOutlineView setFloatsGroupRows:NO];
// Set the row size of the tableview
[_sidebarOutlineView setRowSizeStyle:NSTableViewRowSizeStyleLarge];
// Expand all the root items; disable the expansion animation that normally happens
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0];
[_sidebarOutlineView expandItem:nil expandChildren:YES];
[NSAnimationContext endGrouping];
// Automatically select first row
[_sidebarOutlineView selectRowIndexes:[NSIndexSet indexSetWithIndex:1] byExtendingSelection:NO];
}
return self;
}
나는 또한이 클래스에서 필요한 모든 방법을 가지고 - (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
등 앱 위임의 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
방법 내부
LTSidebarViewController *sidebarViewController = [[LTSidebarViewController alloc] initWithManagedObject:self.managedObjectContext];
내 문제는이, 내가 어떤 오류 및 응용 프로그램 실행을하지 않는 작동하지 않는 것입니다하지만 데이터가 NSOutlineView에 표시되지 않은 : 나는 다음 있습니다.
MainMenu.xib 파일을 처음로드 할 때 자동으로 LTSidebarViewController
클래스의 인스턴스를 만들고이를 init 메소드로 호출하지만 내 init 메소드가 응용 프로그램을 수행하지 않기 때문에 문제가 있음을 알 수 있습니다. 제대로 시작하지 못합니다.
여기 올바른 접근 방법이 있습니까? 간단히 말해서 내가 찾고있는 것은 NSOutlineView의 데이터 소스로 사용되는 별도의 파일을 갖는 것입니다.
그 정보, 특히 AppDelegate의 managedObjectContext에 액세스하는 방법을 알려 주시면 감사하겠습니다. – Martyn
위임자와 데이터 소스로'LTSidebarViewController'를 찾고 있다고 말했어야합니다. 문제는'LTSidebarViewController'가 데이터 소스이기 때문에 AppDelegate 전에 초기화되고 AppDelegate에 필요한 정보 (managedObjectContext)가 작동하지 않기 때문에 나타납니다. – Martyn
그럴 경우 NSApp 대리인의 managedObjectContext에 액세스하면 데이터 소스 메서드가 호출 될 때 LTSidebarViewController가 액세스해야하므로 그 주위를 처리하게됩니다. – ccatalfo