swift - 为什么该操作无法连接到目标类 NSViewController?

标签 swift nsbutton

我正在尝试学习 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 时,会生成如上所示的错误消息。是什么导致了错误,我该如何解决?

最佳答案

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

事实证明,“自定义类”下有一个名为“模块”的小下拉菜单,默认情况下设置为无。将其设置为 tarcrypt(或适合您的任何可用选项),这应该可以修复错误。

感谢大家的帮助!

关于swift - 为什么该操作无法连接到目标类 NSViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35761659/

相关文章:

ios - 构建预填充 Realm 数据库的最佳方式

ios - 从 Storyboard中的 tableview 单元格多次显示 segue

macos - 自 SDK 更新以来 NSButton 不显示替代标题?

swift - 使用 Swift 代码更改 NSButton 的图像

ios - 带有大标题的 UINavigationbar 弹出动画故障

swift - 使用 Swift 的通用 UITableViewDataSource

windows - 寻找一种 cocoa 方法来模拟没有事件作为参数的按钮按下

xcode - 如何在点击一个 NSButton 后保持按下状态?

objective-c - NSImageView 中的图像和按钮位置错误

ios - 如何在 Xcode 9 中在 iOS 设备上运行我的应用程序?