2016-03-03 50 views
1

我努力學習斯威夫特,但我似乎在這(誠然,可能是很簡單的)問題已經得到陷入 - 錯誤如下:爲什麼該操作無法連接到目標類NSViewController?

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時,會生成如上所示的錯誤消息。是什麼導致了錯誤,我該如何解決?

回答

1

我已經想通了!對於遇到此問題的任何其他人,請按以下方法解決:

事實證明,在名爲「模塊」的「自定義類」下有一些下拉菜單,默認情況下它設置爲無。將其設置爲tarcrypt(或適用於您的任何可用選項)並且應該修復錯誤。

感謝您的幫助!

0

聽起來好像您將UI元素連接到文件的所有者對象,該對象是NSApplication的一個實例。

如果您還沒有這樣做,您希望將NSObject從Xcode 4中的對象庫調色板拖出到佈局左側的頁邊距。一旦你完成了,選擇了它,選擇身份檢查器,並在類字段中輸入「WindowController」

相關問題