0

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

当我单击按钮时,它将打开一个DetailWindow带有 NSTableview 的。每个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];
}

详细窗口中

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

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

4

1 回答 1

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

}


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

于 2011-09-26T10:38:37.637 回答