2012-05-10 59 views
0

我正在爲自己編寫一個小型項目時間管理程序,並遇到了令我困惑的問題。對象字符串值設置不正確

它們設置的方式是我有一個名爲TCObject的對象,我在另一個對象ProjectTimerController中使用它。 ProjectTimerControllerNSWindowController,它有它自己的NIB文件。

我正在做的事很簡單。當您點擊NSTableViewProjectTimerController中的一條線時,找到與該線對應的TCObject。然後它會將該信息從TCObject加載到可以查看和編輯某些內容的界面中。
這裏是什麼樣子的截圖:

Screenshot

現在,當我更改文本在NSTextView,然後按Add按鈕-saveString函數被調用和currentSelection(這是一個TCObject,代表了目前選定的行),它的notes變量被設置。我知道當運行setString時,NSLog函數記錄正確的字符串在_notes中,_notes被設置爲新值。在currentSelection被設置爲新選擇的對象之前,-tableViewSelectionDidChange:中記錄了相同的正確字符串。

但是,如果我選擇我剛剛更改註釋的行,它只是加載相同的文本「初始字符串」並檢查_notes告訴我它是「初始字符串」。

事情我沒有isFinished這個問題。當完成複選框切換時,我將相應的TCObjects'isFinished布爾值設置爲與該複選框相同的值。這個對象會記住並根據我選擇的線路進行正確的更改。

[編輯]
*我在這裏增加了一個更清晰的解釋。

  1. 我點擊NSTableView的一個線(可以說最上面的一個)

  2. 這從MyProjects下陣列加載一個相應TCObject和該對象的變量被添加到註釋NSTextView框和成品是打開或關閉。

  3. 如果我現在寫入註釋框並按下「添加」,那麼文本將被設置到該TCObject的_notes變量中。

  4. 所以,如果我點擊另一行一些其他文本加載到註釋框。點擊第一行應該給我在第三步中寫入Notes的字符串,但它不會。當我在-init方法中初始化它時,_notes似乎總是包含我設置的字符串。

  5. 「完成」複選框正常工作。當我單擊該行時,狀態被保存並正確加載。

  6. 我知道,當我按下添加按鈕作爲setString中的NSLog方法時,_notes被正確設置記錄我按下添加按鈕時已寫入Notes的字符串。

[/編輯]

這裏下面是TCObjectProjectTimerController準系統版本。

//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]; 
} 
+0

你可能更簡明地表述你的問題嗎?我不確定你遇到什麼麻煩... –

+0

對不起,昨天我寫了一篇文章時,我有點累了...我會編輯它並添加一個更清晰的解釋。 – Bjorninn

回答

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也改變了或類似的東西...

相關問題