2011-03-13 60 views
1

我想填充一個基於NSTableView的文檔,並使用NSArrayController來控制它。我想我理解了Key Value coding的概念。不過,我擔心NSArrayController不符合Accessor Search Pattern for Ordered Collections。讓我來解釋NSArrayController和關鍵值編碼

我定義一個類名學生

#import <Cocoa/Cocoa.h> 


@interface Student : NSObject { 

    NSString* studentName; 
    float marks; 

} 

//Accessor and mutators 
@property (readwrite, copy) NSString* studentName; 
@property (readwrite) float marks; 

//Initializer - Init all resources 
-(id) init; 

//Dealloc - Release resources 
-(void) dealloc; 

@end 

實施是

#import "Student.h" 


@implementation Student 

//Synthesize the accessors 
@synthesize studentName; 
@synthesize marks; 

//Initializer - Init all resources 
-(id) init 
{ 
    self = [super init]; 
    if(self){ 
     studentName = @"New Student"; 
     marks = 0.0; 
    } 
    return self; 
} 

//Dealloc - Release resources 
-(void) dealloc 
{ 
    [studentName release]; 
    [super dealloc]; 
} 

@end 

myDocument中類的定義如下其中包含一個NSMutableArray型瞬間變

#import <Cocoa/Cocoa.h> 

@class Student; 

@interface MyDocument : NSDocument 
{ 
    NSMutableArray* students; 
} 

//Initializers 
-(id) init; 

//Deallocators 
-(void) dealloc; 

//Creating the proxy object 
-(id) mutableArrayValueForKey:(NSString *)key; 

//Array controller uses keyvalue 
//coding to call this 
-(void) insertObject:(Student*) s inStudentsAtIndex:(int) index; 


@end 

在IB中,設置了陣列控制器的屬性Student對象及其實例變量添加到密鑰中。在綁定部分中,Content Array綁定到File Owner,它是MyDocument類的一個實例。該模型的關鍵路徑設置爲數組名students

這裏是myDocument中

#import "MyDocument.h" 
#import "Student.h" 

@implementation MyDocument 

- (id)init 
{ 
    self = [super init]; 
    if (self) { 

     students = [[NSMutableArray alloc] init]; 

    } 
    return self; 
} 

-(void) dealloc 
{ 
    [students release]; 
    [super dealloc]; 
} 

//Array controller uses keyvalue 
//coding to call this 
-(void) insertObject:(Student*) s inStudentsAtIndex:(int) index 
{ 
    NSLog(@"Insert object is called"); 
} 


//Creating the proxy object 
-(id) mutableArrayValueForKey:(NSString *)key 
{ 
    NSLog(@"Checking if NSArrayController is trying to create a proxy %@",key); 
    return students; 
} 

我的問題-(void) insertObject:(Student*) s inStudentsAtIndex:(int) index的實施不會被調用。但是,如果我實現了名爲-(void) setStudents:(Student*)s的函數,那就是所謂的。 - (id) mutableArrayValueForKey:(NSString *)key僅用於調試目的;我想看到Key Value編碼的某些部分正在工作。行爲是否相同 - (id) mutableArrayValueForKey:(NSString *)key

我錯過了什麼?我在Mac 10.6.6用的XCode 3.2.5

回答

1

請閱讀有序集合部分訪問器搜索模式在here。引用

接收機的類中搜索 對他們的名字方法句子匹配 圖案-insertObject:in<Key>AtIndex: 和 - removeObjectFrom<Key>AtIndex: (對應於的NSMutableArray 原始的方法 insertObject:atIndex:和 removeObjectAtIndex:分別地),或 方法匹配模式 -insert<Key>:atIndexes:-remove<Key>AtIndexes:(對應於 NSMutableArrayinsertObjects:atIndexes:removeObjectsAtIndexes:種方法)。

所以你必須實現PAIR。你的班級應該實施 - removeObjectFrom<Key>AtIndex:insertObject:in<Key>AtIndex:

+0

這工作! – GarryO 2011-03-19 16:28:54

+0

我讀了這3到4次,我告訴自己:「這不是我的問題,不是!我不會這麼做!」 ...但這正是我的問題。謝謝! – Jeff 2013-06-28 19:23:01

1

基於你在說什麼,我不知道什麼時候insertObject:會被調用。在IB,您綁定的陣列控制器到您的NSMutableArray,因此它不知道你MyDocument類東西。

如果你想要某種當一個新的學生被添加到您的陣列的通知,你需要鉤住了自己。要做到這一點的方法之一是處理從UI自己(比如,如果你有一個新的按鈕)的作用,並在處理程序中的對象添加到您的陣列,以及做任何其他的邏輯是必需的。