Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to connect (storyboard) outlet from (NSApplication) to (NSNibExternalObjectPlaceholder) error in Cocoa and storyboard

I've tried to build a sample Cocoa app on which I want to connect UI components put on storyboard to ViewController.swift as either an IBOutlet or IBAction. However, when I tried to control-drag the UI components on storyboard (such as NSButton) to ViewController.swift and create a @IBAction method, and then run the app, the resultant app logs the following message in console and of course the app doesn't respond to me tapping the button.

Failed to connect (storyboard) outlet from (NSApplication) to (NSNibExternalObjectPlaceholder): missing setter or instance variable

How can I use the IBAction method properly?

For your information here's my ViewController.swift:

import Cocoa

class ViewController: NSViewController {
    @IBOutlet var txtTitle : NSTextField
    @IBOutlet var boxColor : NSBox

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func colorChanged(cp: NSColorPanel) {
        let c:NSColor = cp.color;
        self.boxColor.fillColor = c
    }

    @IBAction func btnSetColor(sender : AnyObject) {
        let cp:NSColorPanel = NSColorPanel.sharedColorPanel()
        cp.setTarget(self)
        cp.setAction("colorChanged:")
        cp.orderFront(nil)

    }

    @IBAction func btnSetWindowTitle(sender : AnyObject) {
        if self.txtTitle.stringValue != "" {
            println(self.title)
            println(self.txtTitle.stringValue)
            self.title = self.txtTitle.stringValue
        }
    }
}

I use Xcode 6 beta on OS X 10.10 Yosemite. And started the template with storyboard being on.

like image 880
Blaszard Avatar asked Jun 15 '14 11:06

Blaszard


3 Answers

While the answer above correctly states that this isn't the reason for compilation issues, I thought that I would clarify for those who are just looking to eliminate the warning messages altogether. This was what I was looking for when I found this page.

When you are building your actions and some of the actions change, or get deleted in the storyboard, the outlets remain. Select the controller/window where the older unused actions used to be and you will still see them in the outlets segment of the storyboard within the attributes tab. Remove those old actions/outlets there and then the warning disappear.

Look for duplicates between the ViewController and the File's Owner. One or both might be holding on to these objects when they shouldn't be. Removing those will remove these soft warnings.

like image 138
Jay Snayder Avatar answered Nov 19 '22 17:11

Jay Snayder


Failed to connect (storyboard) outlet from (NSApplication) to (NSNibExternalObjectPlaceholder): missing setter or instance variable

The IBAction methods working like it should, see Apple Dev Forums:

"This is a known issue ... The messages are harmless and do not indicate a problem with your code."

Apple Dev Forums: OS X Storyboard failure

Thats not why your code is not working, you need to fix the following:

A) Here is my working code to set the title - using self.view.window.title instead self.title:

@IBAction func btnSetWindowTitle(sender : AnyObject) {
    if self.txtTitle.stringValue != "" {
        println(self.view.window.title)
        println(self.txtTitle.stringValue)
        self.view.window.title = self.txtTitle.stringValue
    }
}

B) In Interface Builder you need to set NSBox "Box Type" to "Custom": enter image description here

And that's it: enter image description here

like image 5
two Avatar answered Nov 19 '22 17:11

two


I think I figured out the right solution.

enter image description here

1) Drag an Object into you xib interface.

enter image description here

2) Click the Object in the left list you just dragged in.

enter image description here

3) Bind the Object to your custom class.(Here my class is a login window controller as example)

enter image description here

4) Ctrl drag button to the source code. In the popup window, choose your class name(here in example is Login Window Controller) rather than File's Owner.

Hope this could help you.

like image 2
ManuQiao Avatar answered Nov 19 '22 18:11

ManuQiao