在两个 NSWindows 之间切换
我正在制作一个简单的 mac 应用程序,我想在其中切换窗口。
我有两个 NSWindowController 类 MainWindow
和 DetailWindow
我正在使用此代码:
MainWindow 类:
//MainWindow.h
@class DetailWindow;
@interface MainWindow : NSWindowController{
IBOutlet NSButton *btn1;
DetailWindow *detailwindow;
}
@property (nonatomic, retain) IBOutlet NSButton *btn1;
- (IBAction)btn1Event:(id)sender;
//MainWindow.m
@implementation MainWindow
@synthesize btn1;
- (IBAction)btn1Event:(id)sender {
if (!detailwindow) {
detailwindow = [[DetailWindow alloc] initWithWindowNibName:@"DetailWindow"];
}
[detailwindow showWindow:self];
}
@end
DetailWindow 类:
//DetailWindow.h
@class MainWindow;
@interface DetailWindow : NSWindowController{
IBOutlet NSButton *backbtn;
MainWindow *mainwindow;
}
@property (nonatomic, retain) IBOutlet NSButton *backbtn;
- (IBAction)back:(id)sender;
//DetailWindow.m
@implementation DetailWindow
@synthesize backbtn;
- (IBAction)back:(id)sender {
if (!mainwindow) {
mainwindow = [[MainWindow alloc] initWithWindowNibName:@"MainWindow"];
}
[mainwindow showWindow:self];
}
@end
现在问题是当我单击 DetaiWindow 上的 backbtn
> 它将打开一个新的MainWindow
。
所以我在屏幕上有两个 MainWindow
。
当我单击 backbtn 时,我希望主窗口位于前面。
有什么帮助吗?
谢谢..!!
I am making a simple mac app in which i want to switch windows.
I have two NSWindowController class MainWindow
and DetailWindow
I am using this code :
MainWindow class:
//MainWindow.h
@class DetailWindow;
@interface MainWindow : NSWindowController{
IBOutlet NSButton *btn1;
DetailWindow *detailwindow;
}
@property (nonatomic, retain) IBOutlet NSButton *btn1;
- (IBAction)btn1Event:(id)sender;
//MainWindow.m
@implementation MainWindow
@synthesize btn1;
- (IBAction)btn1Event:(id)sender {
if (!detailwindow) {
detailwindow = [[DetailWindow alloc] initWithWindowNibName:@"DetailWindow"];
}
[detailwindow showWindow:self];
}
@end
DetailWindow Class:
//DetailWindow.h
@class MainWindow;
@interface DetailWindow : NSWindowController{
IBOutlet NSButton *backbtn;
MainWindow *mainwindow;
}
@property (nonatomic, retain) IBOutlet NSButton *backbtn;
- (IBAction)back:(id)sender;
//DetailWindow.m
@implementation DetailWindow
@synthesize backbtn;
- (IBAction)back:(id)sender {
if (!mainwindow) {
mainwindow = [[MainWindow alloc] initWithWindowNibName:@"MainWindow"];
}
[mainwindow showWindow:self];
}
@end
Now the problem is when i click backbtn on DetaiWindow
it will open a new MainWindow
.
So i have two MainWindow
on screen.
I want just main window at front when i click backbtn.
Any help??
Thank you..!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的基本问题是每个窗口都假设创建另一个窗口是自己的工作。每个都有一个 ivar 用于另一个,但没有外部访问它 - 通过属性或成为 IBOutlet 或其他任何东西 - 所以它总是以
nil
开始,并且创建一个新副本而不是重用旧副本。有很多方法可以解决这个问题。也许最简单的方法是在 Interface Builder 中创建两个窗口并将它们链接起来,制作 ivars
IBOutlet
。然后您就知道您根本不需要在代码中创建它们。然而,纯粹基于惯性,这里有一个更接近您已有的替代方案。请注意,为简单起见,我假设
mainWindow
始终首先存在。如果没有,您将不得不以相反的方式重复该过程。未经测试,因此通常需要注意。
Your basic problem is that each window is assuming that it is its own job to create the other. Each has an ivar for the other, but there's no external access to it -- via a property or being an
IBOutlet
or anything else -- so it always starts out asnil
, and a new copy gets created instead of reusing the old one.There are any number of ways to get around this. Probably the easiest would be to create both windows in Interface Builder and link them up there, having made the ivars
IBOutlet
. Then you know you never have to create them in code at all.However, purely on the basis of inertia, here's an alternative that sticks closer to what you've got already. Note that I've assumed for simplicity that
mainWindow
always exists first. If not, you'll have to duplicate the process the other way around.Untested, so usual caveats apply.
您必须在主窗口上使用 self 对象调用 orderFront: 方法。
为此,您必须找到对主窗口的引用。一种方法是:
此调用将返回一个指向主窗口的指针(如果您做错了什么,您可能必须循环遍历 [NSApp windows] 数组才能搜索主窗口)。
当您找到窗口后,通过执行以下操作向其发送 orderFront 消息(假设上面的代码返回正确的窗口,如前所述)。
并且窗口应该神奇地排列在前面。
You have to call orderFront: method with self object on main window.
To do this you must find a reference to the main window. A way to do this is:
This call will return you a pointer to your main window (If you did something incorrect, you could have to cycle through the [NSApp windows] array in order to search for your main window).
When you have found the window, send it a orderFront message, by doing (supposing the code above returns the correct window, as explained before).
and the window should magically order front.