我正在爲自己編寫一個小型項目時間管理程序,並遇到了令我困惑的問題。對象字符串值設置不正確
它們設置的方式是我有一個名爲TCObject
的對象,我在另一個對象ProjectTimerController
中使用它。 ProjectTimerController
是NSWindowController
,它有它自己的NIB文件。
我正在做的事很簡單。當您點擊NSTableView
ProjectTimerController
中的一條線時,找到與該線對應的TCObject
。然後它會將該信息從TCObject
加載到可以查看和編輯某些內容的界面中。
這裏是什麼樣子的截圖:
現在,當我更改文本在NSTextView
,然後按Add按鈕-saveString
函數被調用和currentSelection
(這是一個TCObject
,代表了目前選定的行),它的notes變量被設置。我知道當運行setString
時,NSLog函數記錄正確的字符串在_notes中,_notes被設置爲新值。在currentSelection被設置爲新選擇的對象之前,-tableViewSelectionDidChange:
中記錄了相同的正確字符串。
但是,如果我選擇我剛剛更改註釋的行,它只是加載相同的文本「初始字符串」並檢查_notes
告訴我它是「初始字符串」。
事情我沒有isFinished
這個問題。當完成複選框切換時,我將相應的TCObjects
'isFinished
布爾值設置爲與該複選框相同的值。這個對象會記住並根據我選擇的線路進行正確的更改。
[編輯]
*我在這裏增加了一個更清晰的解釋。
我點擊NSTableView的一個線(可以說最上面的一個)
這從MyProjects下陣列加載一個相應TCObject和該對象的變量被添加到註釋NSTextView框和成品是打開或關閉。
如果我現在寫入註釋框並按下「添加」,那麼文本將被設置到該TCObject的_notes變量中。
所以,如果我點擊另一行一些其他文本加載到註釋框。點擊第一行應該給我在第三步中寫入Notes的字符串,但它不會。當我在-init方法中初始化它時,_notes似乎總是包含我設置的字符串。
「完成」複選框正常工作。當我單擊該行時,狀態被保存並正確加載。
我知道,當我按下添加按鈕作爲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];
}
你可能更簡明地表述你的問題嗎?我不確定你遇到什麼麻煩... –
對不起,昨天我寫了一篇文章時,我有點累了...我會編輯它並添加一個更清晰的解釋。 – Bjorninn