【发布时间】:2010-12-14 21:32:35
【问题描述】:
好吧,我做错了什么?
1。创建了 cocoa app 和 appDelegate 命名为:window2AppDelegate
2。 window2AppDelegate.h
#import "PrefWindowController.h"
@interface window2AppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
PrefWindowController * ctrl;
}
@property (assign) IBOutlet NSWindow *window;
- (IBAction) buttonClick:(id)sender;
- (IBAction) buttonCloseClick:(id)sender;
@end
3.在 xib 编辑器中,窗口连接到窗口控制器 - 设置为 appdelegate,buttonclick 动作到按钮
4、创建
#import <Cocoa/Cocoa.h>
@interface PrefWindowController : NSWindowController {
@private
}
@end
#import "PrefWindowController.h"
@implementation PrefWindowController
- (id)init {
self = [super initWithWindowNibName: @"PrefWindow"];
return self;
}
- (void)dealloc {
// Clean-up code here.
[super dealloc];
}
- (void)windowDidLoad {
[super windowDidLoad];
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}
@end
5.创建了名为 PrefWindow 窗口 IBOutlet 的新 xib 文件,该文件从其控制器连接到窗口(也将控制器设置为 PrefWindowController) 选项“启动时可见”未选中!我想在 buttonclick 上看到这个窗口。
6。实现了window2AppDelegate
#import "window2AppDelegate.h"
@implementation window2AppDelegate
@synthesize window;
- (id) init {
if ((self = [super init])) {
ctrl = [[PrefWindowController alloc] init];
if ([ctrl window] == nil)
NSLog(@"Seems the window is nil!\n");
}
return self;
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
return YES;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (IBAction) buttonClick:(id)sender {
// [[ctrl window] makeKeyAndOrderFront:self]; this doesn't work too :(
NSLog(@"it is here");
[ctrl showWindow:sender];
}
- (IBAction) buttonCloseClick:(id)sender {
[window close];
}
@end
7.在我构建并运行应用程序后:closebutton 关闭应用程序但 buttonclick - 不会显示 PrefWindow!?为什么和我做错了什么?不要告诉我在可可objective-c 中显示其他窗口比在“愚蠢的”Java 或C# 中更难?
【问题讨论】:
-
当您进入
-buttonClick:时,您是否检查过以确保ctrl不是nil? -
是的,就像在 init 中一样,但结果相同 - 它是 nil
标签: objective-c cocoa xib nswindowcontroller