切换到另一个窗口时窗口不释放 [英] Window is not releasing when switching to another window

查看:288
本文介绍了切换到另一个窗口时窗口不释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a Mac application in which I have mainwindow with some buttons.

当我点击按钮将打开一个 DetailWindow 与NSTableview。每个 buttonclickevent 都会更改NSTableView中的数据。

When I click button it will open a DetailWindow with NSTableview. Every buttonclickevent change the data in NSTableView.

这是我的代码:

在我的 mainWindow.m p>

In my 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];
}

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 isEqualToString:@First])事件调用,我可以看到我的表格中的前五个图像。

If i click first button this if([title isEqualToString:@"First"]) event call and I can see first five image in my tableview.

我看不到下表中的五个图像。数据不会改变,因为 if([title isEqualToString:@Second])此事件未调用。

如果我第一次单击第二按钮然后同样的事情发生与第一个事件

After that if I click second button I can't see the next five image in table. Data is not changing because if ([title isEqualToString:@"Second"]) this event is not being call.
If I first click second button then same thing happen with first event.

想法为什么?

推荐答案

不,这是因为您的插入图片方法是在 - (void)windowDidLoad方法,它在窗口加载时调用,而不是在调用showWindow:方法时调用,因此只调用一次。而不是在mainWindow.m中调用showWindow:方法,而是在DetailWindow中创建一个新方法,并在单击按钮时调用它。

No, that's because your insert image method is in the -(void)windowDidLoad method, which gets called on when the window is loaded, not when you call the showWindow: method so it only gets called once. Instead of calling the showWindow: method in mainWindow.m, create a new method in DetailWindow instead and call that when you click the button.



- (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];

}


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

It's definitely not the best code you should have. Just make some changes to the code above and it should be sufficient.

这篇关于切换到另一个窗口时窗口不释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆