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

发布于 2024-12-06 15:30:53 字数 3016 浏览 0 评论 0原文

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

当我单击按钮时,它将打开一个带有 NSTableview 的 DetailWindow 。每个 buttonclickevent 都会更改 NSTableView 中的数据。

这是我的代码:

在我的 ma​​inWindow.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"]) 未调用此事件。
如果我首先单击第二个按钮,那么第一个事件也会发生同样的情况。

知道为什么吗?我认为当我第二次单击任何按钮时窗口不会释放。

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

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

Here is my code:

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

In 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 i click first button this if([title isEqualToString:@"First"]) event call and I can see first five image in my tableview.

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.

Any idea why? I think window is not releasing when I click any of button at secondly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘你却要生生世世 2024-12-13 15:30:53

不,那是因为您的插入图像方法位于 -(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];

}


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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文