切换到另一个窗口时,窗口未释放



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

当我点击按钮,它将打开一个DetailWindow与NSTableview。每个buttonclickevent改变NSTableView中的数据。

下面是我的代码:

在我的mainWindow中。m FIle

- (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];
}
在<<p> strong> 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"])事件调用,我可以在我的tableview中看到前五个图像。

之后,如果我点击第二个按钮,我无法看到表中的下五个图像。数据没有变化,因为if ([title isEqualToString:@"Second"])这个事件没有被调用。
如果我先点击second按钮,那么first event也会发生同样的事情。

知道为什么吗?当我再次点击任何一个按钮时,我认为窗口没有释放。

不,那是因为你的insert image方法是在-(void)windowDidLoad方法中,它在窗口加载时被调用,而不是当你调用showWindow:方法时,所以它只被调用一次。而不是调用mainWindow中的showWindow:方法。m,在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]; } 之前

这绝对不是你应该有的最好的代码。只要对上面的代码做一些修改,应该就足够了。

最新更新