我正在尝试学习 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/