我在尋找最簡單的例子,它顯示了一個窗口打開另一個窗口,然後關閉它。可可:創建並關閉另一個窗口
我似乎陷入困境的地方是與NIBs以及如何連接插座。我可以很容易地創建窗口;關閉它是一個挑戰。
任何建議非常感謝!
我在下面附上我的代碼。我認爲我想要做的事很簡單,但它不起作用。感謝弗朗西斯,我現在可以打開和關閉窗口,但是一旦我嘗試再次打開它,應用程序崩潰(EXC_BAD_ACCESS
)。我相信這是由於我對NIB與班級的關係不甚瞭解。在大多數語言中,我只需要實例化一個窗口的新實例,然後關閉它。
我有2個窗口在MainMenu.xib
。 (我寧願將窗口放在單獨的NIB中,但似乎會產生其他問題!)AppDelegate有2個插座,用於窗口(原始)和其他窗口(第2個窗口創建)。
第一窗口具有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
非常重要的細節:這是Mac應用程序,而不是iOS應用程序。 –