Cocoa 切换到其他窗口时,窗口未释放

Cocoa 切换到其他窗口时,窗口未释放,cocoa,nstableview,nswindowcontroller,Cocoa,Nstableview,Nswindowcontroller,我正在创建一个Mac应用程序,其中我有一些按钮的主窗口 当我单击按钮时,它将打开一个带有NSTableview的详细信息窗口。每个按钮ClickEvent更改NSTableView中的数据 这是我的密码: 在mymain window.m文件中 - (IBAction)btn1Event:(id)sender { if (!detailwindow) { detailwindow = [[DetailWindow alloc] initWithWindowNibName:

我正在创建一个Mac应用程序,其中我有一些按钮的主窗口

当我单击按钮时,它将打开一个带有NSTableview的
详细信息窗口
。每个按钮ClickEvent更改NSTableView中的数据

这是我的密码:

在mymain window.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];
}
在详细信息窗口中

- (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 IsequalString:@“first”])
事件调用,我可以在我的tableview中看到前五个图像

之后,如果我点击第二个按钮,我就看不到表中接下来的五幅图像。数据未更改,因为
if([title IsequalString:@“Second”])
未调用此事件。
如果我第一次单击
second
按钮,那么
第一个事件也会发生同样的事情


知道为什么吗?我认为当我点击第二个按钮时,窗口没有释放。

否,这是因为您的插入图像方法位于-(void)windowDidLoad方法中,该方法在加载窗口时被调用,而不是在调用showWindow:方法时被调用,因此它只被调用一次。与其在mainWindow.m中调用showWindow:method,不如在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];

}


这绝对不是你应该拥有的最好的代码。只需对上面的代码进行一些更改,就足够了。

首先非常感谢MateurProgrammer。。!!我试过这个代码。这一次,当我单击按钮时,两个事件都在调用,但表中的图像并没有改变。它们保持不变。我想我必须调用
[detailwindowshowwindow:self]如果我想通过单击事件查看详细信息窗口。。我是ryt吗?哦。您想“更改”它而不是添加数据,是吗?然后使用RemoveObjectsArrangedObjectIndex删除arrayController中的所有数据:要删除对象,请添加对象,数据应该会更改。好的,我注意到一件事。。如果我点击第一个按钮,然后点击第二个按钮,它会在现有图像(第一个按钮的图像)下面添加所有5个图像。所以我在第二次点击中得到了所有10张图片。。有什么帮助吗?就是这样。您正在向其“添加”图像,而没有从中删除图像。基本上,你有5个。你想要一个不同的5,但是,你没有拿走原来的5,所以你最终得到了10。这就是为什么必须使用[arrayController RemoveObjectsArrangedObjectIndex:[NSIndexSet indexSetWithIndex:5]];在实际使用addObject方法之前,我将[arrayController RemoveObjectsAtArrangeDobjectIndex:[NSIndexSet indexSetWithIndex:5]]放入
if([title IsequalString:@“Second”])
上面
addObject
。它仍然在添加图像。。我对mac开发真的很陌生。。给你添了那么多麻烦(