问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/7553370/window-is-not-releasing-when-switching-to-another-window