user3185748
user3185748

Reputation: 2538

Swift class to interact with Interface Builder via IBOutlet

Good evening all,

I'm slowly working through my first OS X app. I have been having a hard time getting my Swift class to interact with an NSPopUpButton. Just to make sure I was doing it right, I created a new project and successfully erased all entries and entered text into the NSPopUpButton via AppDelegate. However, as soon as I try to move the same functionality to my own class, I can't even get the IBOutlet connection across to the new class.

Is a particular subclass type required of a new class to work properly with interface builder?

Here is a screenshot of the class I have created, as well as AppDelegate where I am trying to call the function belonging to this class.

imgur.com/3H7oXRz

Finally, here is the IB element in question, should I be able to select my own class under the 'Custom Class' inspector?

imgur.com/3H7oXRz,24uE4rl#1

Upvotes: 3

Views: 1812

Answers (1)

dcgoss
dcgoss

Reputation: 2207

I am an iOS developer, but I would imagine the same principles would apply to your situation.
A ViewController class and an interface created in interface builder are two seperate things. While it may appear that they are connected via an iboutlet, they are actually independent and one can be instantiated without the other.
Currently, you are only creating an instance of your ViewController class in your App Delegate - and that's all. The system has no idea that your xib even exists! The outlets will only be connected once your app connects your xib to your ViewController class.
How do we do this? It's actually quite simple. Instead of instantiating our view controller like this:

let viewcontroller = ViewController()

We would connect our view controller to our xib in the instantiation:

let viewcontroller = ViewController(nibName: "MainWindow", bundle: NSBundle().mainBundle)

The nibName is telling the system the file name of your xib, and the NSBundle().mainBundle is telling the system where to look for the xib.
This will all only work if your xib has been assigned a custom class, like you mentioned. In your xib in interface builder, select the entire view controller. Then, in the custom class inspector type in the name of your ViewController class (in your case: ViewController - it should autocomplete). This will make sure your outlets are connected.
And you should be set up!! Let me know if you have any more problems come up.
Good luck!

EDIT: This replaces the first part of my answer, however the part about hooking things up in Storyboard remains true. Upon reconsidering, I've believe I've realized that we are only creating the view controller, and not adding it to our view. Despite this, I believe we can take a short cut solution by adding one method to your view controller subclass (the one we set in the Storyboard). Start typing in viewDidLoad, and it should autocomplete. Type in super.viewDidLoad() at the beginning of the method. After that, type self.listUpdate(). This should work if the classes are hooked up correctly in Storyboard. This means you can delete the variables you created in the App Delegate.

Reference: You might also find Apple's documentation on creating a view controller handy (it's in Objective C online, but can be easily converted to Swift - it's the concept that counts): NSViewController Class Reference

Upvotes: 1

Related Questions