Cocoa:从另一个窗口创建并关闭一个窗口

发布于 2024-12-16 12:16:26 字数 1385 浏览 3 评论 0原文

我正在寻找最简单的示例,该示例显示一个窗口打开另一个窗口然后关闭它。

我似乎陷入困境的是NIB 以及插座的接线方式。我可以轻松创建窗口;关闭它是一个挑战。

任何建议非常感谢!

我在下面附上我的代码。我认为我想做的事情很简单,但它不起作用。感谢 Francis,我现在可以打开和关闭窗口,但是一旦我尝试再次打开它,应用程序就会崩溃(EXC_BAD_ACCESS)。我确信这是由于我对 NIB 与类的关系了解不够。在大多数语言中,我只需要实例化一个窗口的新实例,然后关闭它。

我在 MainMenu.xib 中有 2 个窗口。 (我更喜欢将窗口放在单独的 NIB 中,但这似乎会产生其他问题!)AppDelegate 有 2 个出口,即窗口(原始)和 otherWindow(创建的第二个窗口)。

第一个窗口有 2 个按钮:“打开窗口”和“关闭窗口”,并连接到代码中的 2 个方法。

代码:

MyTestAppDelegate.h

#import <Cocoa/Cocoa.h>

@interface MyTestAppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *window;
    NSWindow *otherWindow;
}

@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSWindow *otherWindow;

- (IBAction)openOtherWindow:(id)sender;
- (IBAction)closeOtherWindow:(id)sender;

@end

MyTestAppDelegate.c

#import "MyTestAppDelegate.h"

@implementation MyTestAppDelegate

@synthesize window;
@synthesize otherWindow;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

- (IBAction)openOtherWindow:(id)sender
{
    [otherWindow makeKeyAndOrderFront:sender];
}

- (IBAction)closeOtherWindow:(id)sender
{
    [otherWindow close];
}
@end

I am looking for the simplest example which shows one window opening another window and then closing it.

The place where I seem to get stuck is with the NIBs and how the outlets should be wired. I can easily get the window created; closing it presents a challenge.

Any advice much appreciated!

I'm attaching my code below. I think what I am trying to do is simple, yet it doesn't work. Thanks to Francis I can now get the window to open and close but once I try to open it again the application crashes (EXC_BAD_ACCESS). I'm sure this is due to my poor understanding of the NIB relationship to classes. In most languages I would just need to instantiate a new instance of a window and then close it.

I have 2 windows in MainMenu.xib. (I'd prefer to have the window in a separate NIB but that appears to create other problems!) The AppDelegate has 2 outlets, to window (original) and otherWindow (2nd window created).

The first window has 2 buttons: "Open Window" and "Close Window" with connections to the 2 methods in the code.

Code:

MyTestAppDelegate.h

#import <Cocoa/Cocoa.h>

@interface MyTestAppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *window;
    NSWindow *otherWindow;
}

@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSWindow *otherWindow;

- (IBAction)openOtherWindow:(id)sender;
- (IBAction)closeOtherWindow:(id)sender;

@end

MyTestAppDelegate.c

#import "MyTestAppDelegate.h"

@implementation MyTestAppDelegate

@synthesize window;
@synthesize otherWindow;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

- (IBAction)openOtherWindow:(id)sender
{
    [otherWindow makeKeyAndOrderFront:sender];
}

- (IBAction)closeOtherWindow:(id)sender
{
    [otherWindow close];
}
@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

喜你已久 2024-12-23 12:16:26

基本上,您可以在 NIB 中创建各种窗口,添加按钮、文本字段等。然后添加一个自定义对象,该对象充当“控制器”,用于向窗口和各种控件发送消息以及从窗口和各种控件接收消息。在一个简单的项目中,您可以使用自动创建的 AppDelegate 对象来控制您的窗口,对于更大的项目,您需要一个单独的对象来处理逻辑,该对象可以是 NSObject 或 NSWindowController 的实例,具体取决于您的需要。

Windows 可以设置为“启动时可见”,这会在您的应用程序启动时打开它们。您还可以通过在头文件中创建 IBOutlet 引用并在 NIB 中连接它们来手动打开它们。要显示窗口,您可以向它们发送一条 makeKeyAndOrderFront: 消息。要关闭它们,您可以向它们发送一个 close 方法。

为了响应窗口的打开/关闭,您将控制器对象指定为窗口的“委托”,这意味着它将从窗口接收消息,这些消息在 NSWindowDelegate 协议下的文档中列出。因此,如果您想打开一个窗口来响应另一个窗口的关闭,您可以监听 windowWillClose: 消息并告诉另一个窗口打开,反之亦然。

这是非常基本的内容,所以我建议您阅读 Hillegass 书,或浏览一些各种 教程 可用 在线

编辑:

您的应用程序正在崩溃,因为您将 otherWindow 设置为“关闭时释放”,因为您处于内存管理环境中,并且没有任何内容保留该窗口,所以下次您尝试打开它时,它已经被释放。解决办法是取消勾选NIB文件中的“关闭时释放”。请注意,您还可以直接在 NIB 中将按钮操作连接到其他窗口本身的 makeKeyAndOrderFront:performClose: 方法。

Basically, in your NIB you create the various windows, add your buttons, textfields, etc. Then you add a custom object that acts as a "controller" that sends and receives messages to and from the windows and various controls. In a simple project you can use the automatically-created AppDelegate object to control your windows, for bigger projects you want a separate object to handle the logic, which can be an instance of NSObject or NSWindowController, depending on your needs.

Windows can be set up as "Visible at launch" which opens them when your app launches. You can also open them manually by creating IBOutlet references in the header files and connecting them in the NIB. To show windows you send them a makeKeyAndOrderFront: message. To close them you send them a close method.

To respond to window opening/closing you assign your controller object as the windows' "delegate", which means it will recieve messages from the windows, which are listed in the documentation under the NSWindowDelegate protocol. So if you wanted to open a window in response to another window closing, you would listen for the windowWillClose: message and tell the other window to open, and vice-versa.

This is pretty basic stuff so I recommend you read the Hillegass book, or browse some of the various tutorials available online.

EDIT:

You're application is crashing because you have your otherWindow set to "Release when closed" Since you are in a memory-managing environment, and nothing is retaining the window, the next time you try to open it it has already been freed. The solution is to uncheck "Release When Closed" in the NIB file. Notice you can also connect your button actions to makeKeyAndOrderFront: and performClose: methods of the other window itself, directly in the NIB.

⒈起吃苦の倖褔 2024-12-23 12:16:26

使用 XCode 4.1 中的“UINavigationController”模板或 XCode 4.2 中的“Master-Detail Application”模板创建一个新项目,并查看其中的工作原理。

如果您对通过查看 XCode 模板进行学习不感兴趣,请查看以下教程:http ://www.iosdevnotes.com/2011/03/uinavigationcontroller-tutorial/

Create a new project by using the "UINavigationController" template in XCode 4.1 or the "Master-Detail Application" template in XCode 4.2 and have a look at how the stuff is working there.

If you are not interested in learning by reviewing the XCode template have a look at following tutorial: http://www.iosdevnotes.com/2011/03/uinavigationcontroller-tutorial/

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