Swift 为什么操作无法连接到目标类NSViewController?
我正在尝试学习Swift,但我似乎被这个(诚然,可能很简单)问题所困扰——错误如下:Swift 为什么操作无法连接到目标类NSViewController?,swift,nsbutton,Swift,Nsbutton,我正在尝试学习Swift,但我似乎被这个(诚然,可能很简单)问题所困扰——错误如下: Could not connect action, target class NSViewController does not respond to -(encbutton/decbutton) 这是我的密码。我正在故事板中设计接口,并通过@IB(Outlet/Action)将其连接到代码 出于某种原因,在绘制场景和NSB按钮时,会生成如上所示的错误消息。导致错误的原因是什么?如何修复它?听起来好像您将UI
Could not connect action, target class NSViewController does not respond to -(encbutton/decbutton)
这是我的密码。我正在故事板中设计接口,并通过@IB(Outlet/Action)
将其连接到代码
出于某种原因,在绘制场景和NSB按钮时,会生成如上所示的错误消息。导致错误的原因是什么?如何修复它?听起来好像您将UI元素连接到了文件的所有者对象,该对象是NSApplication的一个实例
如果您还没有这样做,您需要将NSObject从Xcode 4中的对象库调色板拖到布局左侧的空白处。完成此操作并选择它后,选择identity inspector,然后在类字段中输入“WindowController”我已经找到了它!对于遇到此问题的其他人,以下是解决方法: 结果是在“定制类”下有一个小下拉列表,标题为“模块”,默认情况下设置为“无”。将其设置为
tarcrypt
(或任何适合您的可用选项),这将修复错误
谢谢你的帮助 这可能是我遇到的最可笑的令人沮丧的错误。有人知道为什么会这样吗?我在其他我建造的项目中看不到这一点。
// 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
}
}