私は自分のために小さなプロジェクト時間管理プログラムを書いていますが、私を混乱させる問題に遭遇しました。
彼らはそれが設定されている方法は、私がと呼ばTCObject
れる別のオブジェクトで使用すると呼ばれるオブジェクトを持っているということProjectTimerController
です。ProjectTimerController
はであり、NSWindowController
独自のNIBファイルがあります。
私がやっていることはかなり簡単です。検索で行をクリックすると、その行に対応するがNSTableView
ProjectTimerController
検索されます。TCObject
次に、そこから情報をインターフェイスにロードTCObject
し、そこでいくつかのものを表示および編集できます。
これがどのように見えるかのスクリーンショットです:
ここで、テキストを変更してNSTextView
から[追加]ボタンを押すと、-saveString
関数が呼び出されcurrentSelection
(これはTCObject
、現在選択されている行を表します)、そのnotes変数が設定されます。NSLog関数が実行時に_notesにある正しい文字列をログに記録するため、_notesが新しい値として設定されていることを知っていsetString
ます。-tableViewSelectionDidChange:
currentSelectionが新しく選択されたオブジェクトとして設定される直前に 、同じ正しい文字列がログインします。
しかし、メモを変更したばかりの行を選択すると、同じテキスト「初期文字列」が読み込まれ、チェックすると「初期文字列」であること_notes
がわかります。
私はこの問題を抱えていませんisFinished
。TCObjects
[完了]チェックボックスをオンにすると、対応するisFinished
ブール値がチェックボックスと同じ値に設定されます。このオブジェクトは、選択した行に応じて記憶され、正しく変更されます。
[編集]
*ここにわかりやすい説明を追加しました。
NSTableViewの行をクリックします(一番上の行としましょう)
これにより、対応するTCObjectがmyProjects配列からロードされ、そのオブジェクトの変数がNotes NSTextViewボックスに追加され、Finishedがオンまたはオフに切り替えられます。
ここで[メモ]ボックスに書き込み、[追加]を押すと、そのTCObjectの_notes変数に設定されているテキストが表示されます。
したがって、別の行をクリックすると、他のテキストが[メモ]ボックスに読み込まれます。一番上の行をクリックすると、手順3でNotesに書き込んだ文字列が表示されますが、そうではありません。_notesには、-initメソッドで初期化したときに設定した文字列が常に含まれているようです。
「完了」チェックボックスは正常に機能します。クリックすると、線をクリックしたときに状態が正しく保存およびロードされます。
setStringのNSLogメソッドは、[追加]ボタンを押したときにNotesに書き込んだ文字列をログに記録するため、[追加]ボタンを押すと_notesが正しく設定されることを知っています。
[/編集]
以下は、との最低限のバージョンTCObject
ですProjectTimerController
。
//TCObject.h
@interface TCObject : NSObject
{
NSString *_notes;
Boolean _isFinished;
}
@property (retain, nonatomic) NSString *notes;
@property (nonatomic) Boolean isFinished;
@end
//TCObject.m
#import "TCObject.h"
@implementation TCObject
@synthesize notes = _notes, isFinished = _isFinished;
-(id)init{
if (self = [super init]) {
self.notes = @"Initial string";
self.isFinished = NO;
}
return self;
}
- (void)dealloc {
[_notes release]; _notes = nil;
[super dealloc];
}
-(void)setNotes:(NSString *)notes {
[notes retain];
[_notes release];
_notes = notes;
NSLog(@"Setting _notes as: %@", _notes);
}
-(NSString *)notes {
NSLog(@"Getting _notes, which is: %@", _notes);
return _notes;
}
@end
//ProjectTimerController.m
- (id)initWithWindow:(NSWindow *)window {
self = [super initWithWindow:window];
if (self)
{
myProjects = [[NSMutableArray alloc]init];
currentSelection = nil;
TCObject *newProject = [[TCObject alloc] init];
TCObject *newProject2 = [[TCObject alloc] init];
TCObject *newProject3 = [[TCObject alloc] init];
TCObject *newProject4 = [[TCObject alloc] init];
[myProjects addObject:newProject];
[myProjects addObject:newProject2];
[myProjects addObject:newProject3];
[myProjects addObject:newProject4];
}
return self;
}
-(IBAction)isFinishedToggle:(id)sender {
if(currentSelection != nil){
currentSelection.isFinished = finishedCheckBtn.state;
}
}
-(IBAction)saveString:(id)sender {
if(currentSelection != nil){
currentSelection.notes = [[notesField textStorage] string];
}
}
//delegate function for NSTableView
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification {
NSInteger selectedIndex = [table selectedRow];
if(selectedIndex == -1){
return;
}
//here the correct notes string is printed
NSLog(@"curr: %i", currentSelection.notes);
currentSelection = [myProjects objectAtIndex:selectedIndex];
NSString *notesInfo = currentSelection.notes;
Boolean isFinishedInfo = currentSelection.isFinished;
[notesField setString:notesInfo];
[finishedCheckBtn setState:isFinishedInfo];
}