为什么该操作无法连接到目标 class NSViewController?

Why would the action not be able to connect to target class NSViewController?

我正在努力学习Swift,但我似乎一直卡在这个(诚然,可能很简单)问题上——错误如下:

Could not connect action, target class NSViewController does not respond to -(encbutton/decbutton)

这是我的代码。我正在 Storyboard 中设计我的界面,并通过 @IB(Outlet/Action).

将其连接到代码
// ViewController.swift
import Cocoa
import Foundation

class TabViewController: NSTabViewController {
// This has been changed from NSViewController to NSTabViewController as I have replaced the initial single-view with a two-tab-view.

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }

}

public class EncViewController: NSViewController {
    // This is for the first tab, encrypt

    @IBOutlet var encdirfield: NSTextField!
    @IBOutlet var encpassfield: NSSecureTextField!
    @IBOutlet var enclogfield: NSTextField!

    @IBOutlet var encbutton: NSButton!

    @IBAction func startenc(sender: NSButton) { // here's the problem. the function isn't triggered when the button is pressed
        // get values
        encdir = encdirfield.stringValue
        encpass = encpassfield.stringValue

        tarcrypt.enc();
        // this is an function that leads to an NSTask that runs a binary I wrote (not related).
        // definitely not the cause of the problem because running it independently works fine
    }


}

public class DecViewController: NSViewController {
    // and this is for the second tab, decrypt      

    @IBOutlet var decdirfield: NSTextField!
    @IBOutlet var decpassfield: NSSecureTextField!
    @IBOutlet var declogfield: NSTextField!

    @IBOutlet var decbutton: NSButton!
    @IBAction func startdec(sender: NSButton) { // here's the same problem, again. the function isn't triggered when the button is pressed
        // get values
        encdir = encdirfield.stringValue
        encpass = encpassfield.stringValue

        tarcrypt.dec();
        // this is an function that leads to an NSTask that runs a binary I wrote (not related).
        // definitely not the cause of the problem because running it independently works fine
    }

}

出于某种原因,在绘制场景和 NSButton 时,会生成如上所示的错误消息。是什么导致了错误,我该如何解决?

听起来好像您将 UI 元素连接到 File's Owner 对象,它是 NSApplication 的一个实例。

如果您还没有这样做,您希望将 NSObject 从 Xcode 4 中的对象库面板拖到布局左侧的空白处。完成并选择它后,选择身份检查器,然后在 Class 字段中输入 "WindowController"

我想通了!对于遇到此问题的任何其他人,以下是解决方法:

原来 "Custom Class" 下有一个标题为 "Module" 的小下拉菜单,默认情况下设置为 none。将其设置为 tarcrypt(或任何适合您的可用选项),这应该可以修复错误。

感谢大家的帮助!

Swift 5 和 Xcode 13.3

最近我遇到了同样的错误。我通过将 viewcontroller class 名称重新分配给 nameclass.swift 文件中的 class 并使用我的项目激活 MODULE 条目来解决它名称(在下拉菜单之后)。