0

私は自分のために小さなプロジェクト時間管理プログラムを書いていますが、私を混乱させる問題に遭遇しました。

彼らはそれが設定されている方法は、私がと呼ばTCObjectれる別のオブジェクトで使用すると呼ばれるオブジェクトを持っているということProjectTimerControllerです。ProjectTimerControllerはであり、NSWindowController独自のNIBファイルがあります。

私がやっていることはかなり簡単です。検索で行をクリックすると、その行に対応するがNSTableView ProjectTimerController検索されます。TCObject次に、そこから情報をインターフェイスにロードTCObjectし、そこでいくつかのものを表示および編集できます。
これがどのように見えるかのスクリーンショットです:

スクリーンショット

ここで、テキストを変更してNSTextViewから[追加]ボタンを押すと、-saveString関数が呼び出されcurrentSelection(これはTCObject、現在選択されている行を表します)、そのnotes変数が設定されます。NSLog関数が実行時に_notesにある正しい文字列をログに記録するため、_notesが新しい値として設定されていることを知っていsetStringます。-tableViewSelectionDidChange:currentSelectionが新しく選択されたオブジェクトとして設定される直前に 、同じ正しい文字列がログインします。

しかし、メモを変更したばかりの行を選択すると、同じテキスト「初期文字列」が読み込まれ、チェックすると「初期文字列」であること_notesがわかります。

私はこの問題を抱えていませんisFinishedTCObjects[完了]チェックボックスをオンにすると、対応するisFinishedブール値がチェックボックスと同じ値に設定されます。このオブジェクトは、選択した行に応じて記憶され、正しく変更されます。

[編集]
*ここにわかりやすい説明を追加しました。

  1. NSTableViewの行をクリックします(一番上の行としましょう)

  2. これにより、対応するTCObjectがmyProjects配列からロードされ、そのオブジェクトの変数がNotes NSTextViewボックスに追加され、Finishedがオンまたはオフに切り替えられます。

  3. ここで[メモ]ボックスに書き込み、[追加]を押すと、そのTCObjectの_notes変数に設定されているテキストが表示されます。

  4. したがって、別の行をクリックすると、他のテキストが[メモ]ボックスに読み込まれます。一番上の行をクリックすると、手順3でNotesに書き込んだ文字列が表示されますが、そうではありません。_notesには、-initメソッドで初期化したときに設定した文字列が常に含まれているようです。

  5. 「完了」チェックボックスは正常に機能します。クリックすると、線をクリックしたときに状態が正しく保存およびロードされます。

  6. 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];
}
4

1 に答える 1

0

ついに問題を発見した。このようにノートを変更するようです:

-(IBAction)saveString:(id)sender {
    if(currentSelection != nil){
        currentSelection.notes = [[notesField textStorage] string];
    }
}

いくつかの問題を引き起こします。私がこのようにすれば、すべてがうまくいきます:

-(IBAction)saveString:(id)sender{
    if(currentSelection != nil){
        NSString *temps = [NSString stringWithFormat:@"%@", [[notesField textStorage] string]];
        currentSelection.notes = temps;
    }
}

つまり、何が起こっているのかは、_notesがNSTextViewに含まれているテキストを指していることだと思います。したがって、テキストを変更すると、_notesも変更されたか、そのようなものになります...

于 2012-05-11T16:18:19.327 に答える