2011-09-26 99 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

相关问题