在两个 NSWindows 之间切换

发布于 2024-12-05 03:49:57 字数 1383 浏览 5 评论 0原文

我正在制作一个简单的 mac 应用程序,我想在其中切换窗口。
我有两个 NSWindowController 类 MainWindowDetailWindow

我正在使用此代码:

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 技术交流群。

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

发布评论

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

评论(2

冷弦 2024-12-12 03:49:57

您的基本问题是每个窗口都假设创建另一个窗口是自己的工作。每个都有一个 ivar 用于另一个,但没有外部访问它 - 通过属性或成为 IBOutlet 或其他任何东西 - 所以它总是以 nil 开始,并且创建一个新副本而不是重用旧副本。

有很多方法可以解决这个问题。也许最简单的方法是在 Interface Builder 中创建两个窗口并将它们链接起来,制作 ivars IBOutlet。然后您就知道您根本不需要在代码中创建它们。

然而,纯粹基于惯性,这里有一个更接近您已有的替代方案。请注意,为简单起见,我假设 mainWindow 始终首先存在。如果没有,您将不得不以相反的方式重复该过程。

//MainWindow.h
@class DetailWindow;
@interface MainWindow : NSWindowController
{

    IBOutlet NSButton *btn1;
    DetailWindow *detailwindow;
}
@property (nonatomic, retain) NSButton *btn1;
- (IBAction)btn1Event:(id)sender;

//MainWindow.m

@implementation MainWindow
@synthesize btn1;

- (IBAction)btn1Event:(id)sender
{
    if (!detailwindow)
    {
        detailwindow = [[DetailWindow alloc] initWithWindowNibName:@"DetailWindow"];

        // having created the other window, give it a reference back to this one
        detailWindow.mainWindow = self;
    }

    [detailwindow showWindow:self];
}
@end

//DetailWindow.h
@class MainWindow;
@interface DetailWindow : NSWindowController
{

    IBOutlet NSButton *backbtn;
    MainWindow *mainwindow;
}
@property (nonatomic, retain) NSButton *backbtn;

// allow the main window to be set from outside
@property (nonatomic, retain) MainWindow *mainWindow;
- (IBAction)back:(id)sender;

//DetailWindow.m

@implementation DetailWindow
@synthesize backbtn;
@synthesize mainWindow;

- (IBAction)back:(id)sender
{
    // no window creation on the way back
    NSAssert(mainWindow, "mainWindow not set!");
    [mainwindow showWindow:self];
}
@end

未经测试,因此通常需要注意。

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 as nil, 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.

//MainWindow.h
@class DetailWindow;
@interface MainWindow : NSWindowController
{

    IBOutlet NSButton *btn1;
    DetailWindow *detailwindow;
}
@property (nonatomic, retain) NSButton *btn1;
- (IBAction)btn1Event:(id)sender;

//MainWindow.m

@implementation MainWindow
@synthesize btn1;

- (IBAction)btn1Event:(id)sender
{
    if (!detailwindow)
    {
        detailwindow = [[DetailWindow alloc] initWithWindowNibName:@"DetailWindow"];

        // having created the other window, give it a reference back to this one
        detailWindow.mainWindow = self;
    }

    [detailwindow showWindow:self];
}
@end

//DetailWindow.h
@class MainWindow;
@interface DetailWindow : NSWindowController
{

    IBOutlet NSButton *backbtn;
    MainWindow *mainwindow;
}
@property (nonatomic, retain) NSButton *backbtn;

// allow the main window to be set from outside
@property (nonatomic, retain) MainWindow *mainWindow;
- (IBAction)back:(id)sender;

//DetailWindow.m

@implementation DetailWindow
@synthesize backbtn;
@synthesize mainWindow;

- (IBAction)back:(id)sender
{
    // no window creation on the way back
    NSAssert(mainWindow, "mainWindow not set!");
    [mainwindow showWindow:self];
}
@end

Untested, so usual caveats apply.

夏夜暖风 2024-12-12 03:49:57

您必须在主窗口上使用 self 对象调用 orderFront: 方法。

为此,您必须找到对主窗口的引用。一种方法是:

[NSApp mainWindow];

此调用将返回一个指向主窗口的指针(如果您做错了什么,您可能必须循环遍历 [NSApp windows] 数组才能搜索主窗口)。

当您找到窗口后,通过执行以下操作向其发送 orderFront 消息(假设上面的代码返回正确的窗口,如前所述)。

[[NSApp mainWindow] orderFront:self];

并且窗口应该神奇地排列在前面。

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:

[NSApp mainWindow];

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).

[[NSApp mainWindow] orderFront:self];

and the window should magically order front.

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