切换到另一个窗口时窗口未释放
我正在创建一个 Mac 应用程序,其中有带有一些按钮的主窗口。
当我单击按钮时,它将打开一个带有 NSTableview 的 DetailWindow
。每个 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];
}
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,那是因为您的插入图像方法位于 -(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.
It's definitely not the best code you should have. Just make some changes to the code above and it should be sufficient.