【问题标题】:Improper setup of window displayed through menu item?通过菜单项显示的窗口设置不正确?
【发布时间】:2012-09-01 11:10:05
【问题描述】:

在玩了一段时间 iOS 之后,我正在开发我的第一个原生 mac 应用程序。

我试图从一个菜单项启动一个窗口,但我怀疑我做错了。任何IBAction 我连接到这个新窗口上的按钮都会返回错误。

这就是我正在做的事情。从一个菜单项我启动这个:

-(IBAction)displaySAInput:(id)sender{

NSLog(@"Input selected.");
inputSAViewController = [[NSWindowController alloc] initWithWindowNibName:@"InputViewController"];
[inputSAViewController showWindow:self];

这将启动 InputViewController 类拥有的 InputViewController nib。我将InputViewController 类设置为继承自NSWindowController

InputViewController.m我已经测试过IBActions 比如:

-(IBAction)testButton:(id)sender{

NSLog(@"Data recalled?");

}

我通过 Interface Builder 将此 IBAction 连接到一个按钮。一切看起来都很好。

当我构建并打开 InputViewController 窗口时,我在控制台中收到此错误,然后单击任何内容:

Could not connect the action testButton: to target of class NSWindowController

我进行了广泛的搜索,但我的无知使我无法将这些点联系起来。这个线程based on a similar error with NSApplication 看起来很有希望,但我不太明白我需要什么才能使与NSWindowController 错误相关的连接发生。

这应该很简单。我错过了什么?

【问题讨论】:

    标签: objective-c macos cocoa


    【解决方案1】:

    您的代码:

    -(IBAction)displaySAInput:(id)sender{
        NSLog(@"Input selected.");
        inputSAViewController = [[NSWindowController alloc]
                      initWithWindowNibName:@"InputViewController"];
        [inputSAViewController showWindow:self];
    }
    

    请注意,您正在分配/初始化NSWindowController 的通用实例,而不是您实现testButton: 方法的自定义子类。我假设您可能希望将其更改为:

    -(IBAction)displaySAInput:(id)sender{
        NSLog(@"Input selected.");
        inputSAViewController = [[InputViewController alloc]
                      initWithWindowNibName:@"InputViewController"];
        [inputSAViewController showWindow:self];
    }
    

    【讨论】:

    • 这听起来很有希望...第一次尝试时出现链接器错误。努力解决。
    • 这是我仍然收到的错误...架构 x86_64 的未定义符号:“_OBJC_CLASS_$_InputViewController”,引用自:ReportController.old 中的 objc-class-ref:符号不是为架构 x86_64 clang 找到:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
    • 已解决链接器问题(ReportController.m 未在目标编译列表中列出)但在更新到您的建议后仍然收到错误:无法将操作 testButton: 连接到类 InputViewController 的目标
    【解决方案2】:

    只需事先加载 NSWindow 并使其不可见,当调用 IBAction 时,只需执行以下操作:

    [myWindow setIsVisible:YES];
    [myWindow setLevel:NSFloatingWindowLevel];
    

    耶!

    【讨论】:

    • 只是为了确保我理解,你是说我应该让它位于菜单本身的文件所有者下,而不是从新类启动它?然后只需切换其可见性?
    • 是的,至少这是我所做的并且效果很好。
    • 我最终选择了这条路线。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 2014-02-02
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多