为什么动作无法连接到目标类NSViewController?

时间:2016-03-03 01:50:23

标签: swift nsbutton

我试图学习Swift,但我似乎已经陷入了这个问题(不可否认,可能非常简单) - 错误如下:

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

这是我的代码。我在故事板中设计我的界面,并通过@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一起绘制时,会生成如上所示的错误消息。导致错误的原因是什么,我该如何解决?

2 个答案:

答案 0 :(得分:3)

我已经明白了!对于遇到此问题的任何其他人,以下是如何修复它:

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

感谢您的帮助!

答案 1 :(得分:0)

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

如果您还没有这样做,则需要将NSObject从Xcode 4中的Object Library面板拖出到布局左侧的边距。完成后,选择它,选择身份检查器,然后在Class字段中输入“WindowController”