perhapsmaybeharry
perhapsmaybeharry

Reputation: 894

Why would the action not be able to connect to target class NSViewController?

I'm trying to learn Swift, but I seem to have gotten stuck at this (admittedly, probably very simple) problem - the error as follows:

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

Here is my code. I'm designing my interface in the Storyboard and connecting it to the code through @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
    }

}

For some reason, upon drawing the scene along with the NSButton, the error message as seen above is generated. What is causing the error, and how do I fix it?

Upvotes: 2

Views: 1920

Answers (3)

Alessandro Ornano
Alessandro Ornano

Reputation: 35392

Swift 5 and Xcode 13.3

Recently I had the same bug. I solved it by reassigning the viewcontroller class name to the class in my nameclass.swift file and activating the MODULE entry with my project name (following the dropdown menu).

Upvotes: 0

perhapsmaybeharry
perhapsmaybeharry

Reputation: 894

I've figured it out! For anyone else who runs into this problem, here's how to fix it:

It turns out there is a little dropdown under "Custom Class" titled "Module", which is, by default, set to none. Set it to tarcrypt (or whichever available option suits you) and that should fix the errors.

Thanks for all the help!

Upvotes: 3

Satyendra Chauhan
Satyendra Chauhan

Reputation: 328

It sounds as if you connected your UI element to the File's Owner object, which is an instance of NSApplication.

If you haven't done so already, you want to drag a NSObject out of the Object Library palette in Xcode 4 to the margin to the left of your layout. Once you've done that, and have selected it, choose the identity inspector and, in the Class field, enter "WindowController"

Upvotes: 0

Related Questions