gpt4 book ai didi

objective-c - 无法关闭窗口 - 为什么?

转载 作者:行者123 更新时间:2023-12-03 17:31:33 27 4
gpt4 key购买 nike

我有一个应用程序,当在单独的窗口中单击或关闭复选框时,应打开和关闭一个窗口。我可以打开它,但无法关闭它。我在 windowControllerObject 中定义了一个 NSWindow 并尝试关闭该 NSWindow。相关代码为:

buttonController.h

@interface buttonController : NSWindowController
{
NSButton *showAnswerBox;
infoWindowController *answerWindowController;
}
- (IBAction)showAnswer:(id)sender;
@end

按钮 Controller .m

- (IBAction) showAnswer:(id) sender
{
if ([sender state] == NSOnState) {
if (!answerWindowController) {
answerWindowController = [[infoWindowController alloc] init];
}
[answerWindowController showWindow:self];
}
else {
[answerWindowController hideWindow];
}
}

infoWindowController.h:

@interface infoWindowController : NSWindowController {

IBOutlet NSWindow * infoWindow;
}
- (id) init;
- (NSWindow *) window;
- (void) hideWindow;
- (void) tsSetTitle: (NSString *) displayName;

@end

在 infoWindowController.m 中:

- (NSWindow *) window
{
return infoWindow;
}

- (void) hideWindow
{
[[self window] close];
}

窗口会打开,但不会关闭。我尝试了几种变体,包括 infoWindowController 上的 orderOut。我确信我错过了一些愚蠢的东西 - 它是什么?

在 IB 中,我什至可以打开窗口的唯一方法是选中“启动时打开”——如果没有选中“打开时打开”,我是否应该能够以编程方式打开它们?

最佳答案

NSWindowController 已经定义了 window 属性。通过实现您自己的 -window 方法,您已经有效地覆盖了该属性的 getter。不过,setter 仍然是继承的版本。

因此,假设您已将 Controller 的 window 导出连接到 NIB 中的窗口,则会调用继承的 setter。这使得 -showWindow: 的继承实现能够显示窗口。但是您的 -window 方法将返回 nil,因为继承的 setter 不会设置您的 infoWindow 实例变量。

摆脱单独的 infoWindow 属性和 getter。只需使用继承的 window 属性及其访问器即可。

关于objective-c - 无法关闭窗口 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19205808/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com