2011-09-26 78 views
0

我正在創建一個Mac應用程序,其中有一些按鈕的主窗口。切換到另一個窗口時窗口不會釋放

當我點擊按鈕,它將打開一個帶有NSTableview的DetailWindow。每個buttonclickevent更改NSTableView中的數據。

這裏是我的代碼:

在我mainWindow.m文件

- (IBAction)btn1Event:(id)sender { 
    if (!detailwindow) { 
     detailwindow = [[DetailWindow alloc] initWithWindowNibName:@"DetailWindow"]; 
     detailwindow.mainwindow = self; 
    } 

    detailwindow.title = @"First"; 
    [detailwindow showWindow:self]; 
} 

- (IBAction)btn2Event:(id)sender{ 
    if (!detailwindow) { 
     detailwindow = [[DetailWindow alloc] initWithWindowNibName:@"DetailWindow"]; 
     detailwindow.mainwindow = self; 
    } 

    detailwindow.title = @"Second"; 
    [detailwindow showWindow:self]; 
} 

DetailWindow

- (void)windowDidLoad 
{ 
    [super windowDidLoad]; 
    [tableView setBackgroundColor:[NSColor clearColor]]; 
    [tableView setHeaderView:nil]; 

    if([title isEqualToString:@"First"]){ 
     [arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"MS.png"],@"image",@"first image",@"text", nil]]; 
     [arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"CVS.png"],@"image",@"second image",@"text", nil]]; 
     [arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"NS.png"],@"image",@"first image",@"text", nil]]; 
     [arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"EM.png"],@"image",@"second image",@"text", nil]]; 
     [arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"RES.png"],@"image",@"first image",@"text", nil]]; 
    } 

    if ([title isEqualToString:@"Second"]) { 
     [arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"RS.png"],@"image",@"second image",@"text", nil]]; 
     [arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"FB.png"],@"image",@"first image",@"text", nil]] ; 
     [arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"GT.png"],@"image",@"second image",@"text", nil]]; 
     [arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"SKIN.png"],@"image",@"first image",@"text", nil]]; 
     [arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"GP.png"],@"image",@"second image",@"text", nil]]; 
    } 

    [tableView reloadData]; 
} 

- (IBAction)GoToMainWindow:(id)sender { 
    [mainwindow showWindow:self]; 
} 

如果我點擊第一個按鈕,這個if([title isEqualToString:@"First"])事件電話,我可以在我的桌面視圖中看到前五個圖像。

之後,如果我點擊第二個按鈕,我看不到表中的下五個圖像。數據沒有變化,因爲if ([title isEqualToString:@"Second"])這個事件沒有被調用。
如果我第一次點擊second按鈕,那麼first event也會發生同樣的情況。

任何想法爲什麼?其次,當我點擊任何一個按鈕時,我認爲窗口不會釋放。

回答

0

不,這是因爲你的插入圖像方法在 - (void)windowDidLoad方法中,當窗口被加載時調用它,而不是當你調用showWindow:方法,所以它只被調用一次。而不是調用mainWindow.m中的showWindow:方法,而是在DetailWindow中創建一個新方法,並在點擊按鈕時調用該方法。

 

- (IBAction)btn1Event:(id)sender { 

if (!detailwindow) { 
    detailwindow = [[DetailWindow alloc] initWithWindowNibName:@"DetailWindow"]; 
    detailwindow.mainwindow = self; 

} 
detailwindow.title = @"First"; 
[detailwindow addImageToTableView]; //It doesn't have to be named like that. Your choice 
} 

- (IBAction)btn2Event:(id)sender{ 

if (!detailwindow) { 
    detailwindow = [[DetailWindow alloc] initWithWindowNibName:@"DetailWindow"]; 
    detailwindow.mainwindow = self; 
} 
detailwindow.title = @"Second"; 
[detailwindow addImageToTableView]; 

//DetailWindow 

- (void)addImageToTableView 
{ 
[super windowDidLoad]; 
[tableView setBackgroundColor:[NSColor clearColor]]; 
[tableView setHeaderView:nil]; 

if([title isEqualToString:@"First"]){ 

[arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"MS.png"],@"image",@"first image",@"text", nil]]; 
[arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"CVS.png"],@"image",@"second image",@"text", nil]]; 
[arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"NS.png"],@"image",@"first image",@"text", nil]]; 
[arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"EM.png"],@"image",@"second image",@"text", nil]]; 
[arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"RES.png"],@"image",@"first image",@"text", nil]]; 

} 

if ([title isEqualToString:@"Second"]) { 

[arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"RS.png"],@"image",@"second image",@"text", nil]]; 
[arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"FB.png"],@"image",@"first image",@"text", nil]] ; 
[arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"GT.png"],@"image",@"second image",@"text", nil]]; 
[arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"SKIN.png"],@"image",@"first image",@"text", nil]]; 
[arrayController addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSImage imageNamed:@"GP.png"],@"image",@"second image",@"text", nil]]; 

} 
[tableView reloadData]; 
} 
- (IBAction)GoToMainWindow:(id)sender { 

[mainwindow showWindow:self]; 

} 

 

這絕對不是你應該擁有的最好的代碼。只需對上面的代碼進行一些更改即可,這應該足夠了。

+0

首先非常感謝你的業餘程序員。我試過這段代碼。這次當我點擊按鈕時,兩個事件都在調用,但**圖像在表格**中沒有改變。他們保持不變。我想我必須調用'[detailwindow showWindow:self];'如果我想單擊事件的detailWindow ..我是ryt? – iUser

+1

哦。你想「改變」它而不是添加數據嗎?然後使用removeObjectsAtArrangedObjectIndexes刪除arrayController中的所有數據:刪除對象,然後添加對象,數據應該更改。 – TheAmateurProgrammer

+0

好吧,我注意到一件事..如果我點擊第一個按鈕,然後第二個按鈕它添加它的所有5圖像下方的現有圖像(第一個按鈕的圖像)。所以我得到所有10個圖像在第二次點擊..任何幫助? – iUser

相關問題