好吧,我做错了什么?
1.创建了cocoa app和appDelegate,命名为:window2AppDelegate
2. 窗口AppDelegate.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 的新 xib 文件,窗口 IBOutlet 从其控制器连接到窗口(也将控制器设置为 PrefWindowController)选项“启动时可见”未选中!我想在单击按钮时看到这个窗口。
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 关闭应用程序,但单击按钮 - 不会向我显示 PrefWindow!?为什么以及我做错了什么?不要告诉我在 cocoa Objective-C 中显示其他窗口比在“愚蠢的”Java 或 C# 中更困难吗?
终于我解决了这个问题!在 PrefWindow 的笔尖编辑器中,我必须执行以下操作:将文件的所有者类设置为:NSWindowController then 将文件所有者的窗口 IBOutlet 连接到我的(首选项)窗口。经过 6 天的尝试失败后,Google 成功了。
无论如何,感谢您的所有回复和时间!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)