IBOutlets in Swift are nil
Asked Answered
M

3

12

(Developing on OSX 10.9, Xcode 6.1b)

If I declare IBOutlets in my AppController, everything is fine. I instantiate an object in InterfaceBuilder, drag it across to form an outlet, and by the time I have reached applicationDidFinishLaunching, my IBOutlets are populated and everything is great.

If I go a step further in abstraction and instantiate a custom controller object in InterfaceBuilder (a subclass of NSObject), and declare one of my objects as an IBOutlet in that class, they're nil, each and every one.

I can set the connection just fine, IB seems convinced it exists, the 'referenced outlets' list is correct, but it doesn't take, and I haven't been able to find anything in the documentation beyond

It is implicitly unwrapped because after your class is initialized from a storyboard or xib file, you can assume that the outlet has been connected.

Well, I was assuming that.

All of my code is boilerplate Xcode offerings:

@IBOutlet weak var sceneController: NSArrayController!

and I've checked and double-checked and triple-checked the connections. I've looked at dozens of iOS tutorials (although I cannot find the equivalent for OSX) all of which seems to be variations on the theme of 'yes, you can totally declare an outlet in a file other than the AppController, just make sure that every involved instance exists'.

(At the time of writing, the 'mac' documentation uses examples featuring UIButton etc.)

I'm out of ideas. It's obvious that the connection is not formed, presumably because the objects are instantiated in an order other than 'controller class first, IBOutlets later', but how can I force this connection?

Motionless answered 19/9, 2014 at 17:53 Comment(2)
I have the same problem. Have you found any solution ?Beth
See my comment below, adding @objc to the receiving class and running 'clean' did help. (I haven't seen this since). I am currently (6.1) getting a lot of 'cannot find details for class x' (including the AppController class) which I circumvent by writing the IBAction/IBOutlet code, running 'clean', and making the connection from class to nib file.)Motionless
W
5
@IBOutlet weak var sceneController: NSArrayController!

The weak keyword is your problem. If, after the system finishes decoding your nib, nothing else references the NSArrayController, then the system will immediately set the outlet to nil and deallocate the NSArrayController.

Try removing the weak keyword from your outlets.

UPDATE

Add this code:

@IBOutlet var sceneController: NSArrayController! {
    didSet {
        NSLog("sceneController set to %@", sceneController);
    }
}

What's the output? Put a breakpoint on the NSLog. What's the stack trace when it's hit? Is it hit repeatedly?

Wiliness answered 19/9, 2014 at 17:57 Comment(6)
Thanks for the suggestion, this is one thing I hadn't tried since my understanding was that instantiating an item in the nib file means that the nib file has a strong reference to the item. Sadly, this isn't the solution: I know the controller exists because it populates a table, and removing 'weak' makes no difference, so this is about the outlet not being set.Motionless
Put a breakpoint on the setter method and see if it's called.Wiliness
There isn't a setter method. As far as I understand, InterfaceBuilder is supposed to resolve the IBOutlet directive once the nib has finished loading, and it doesn't. I have never seen a method to set an IBOutlet in code, so the only workaround seems to be to force instantiation in applicationDidFinishLaunching and setting up connections manually? That would make for a lot of very messy code :-(Motionless
using 'didSet' on the IBOutlet did help me to check that no, they really weren't set. Every time I tested this Apple brought out a new version of Xcode, and GM1 gave me a lot of problems. In GM2, error messages have changed. Two seemingly necessary steps: use @objc(className) for the class you want have the IBOutlet property and use 'clean' to make that change stick, otherwise IB will not recognise that class. I would suspect this was the problem all along, only now the error messages were clearer.Motionless
weak is ok. I had @objc but didn't have @objc(className). Apparently (className) is needed which is ridiculous.Staurolite
Thank you Rob +1 :)Sena
C
1

I had a similar problem

I have a button that takes me from one view controller to another. Inside the event handler for this button I was trying to set values for some of the GUI components in the second view. Unfortunately, I was getting a nil value for those components.

The solution was to wait until the button handler exited then make changes to the GUI components in the second view (try the viewDidLoad() function on the second view controller). Presumably those GUI components were not allocated until I left the handler and switched views.

yuen helbig

Coursing answered 22/2, 2015 at 2:1 Comment(0)
E
1

I had the issue when I was configuring the corner radius of the buttons in viewWillAppear. Moving this part to viewDidLoad solved the issue.

Efficiency answered 20/1, 2020 at 16:49 Comment(1)
I'll make a note of that - I haven't had this problem in years (and several Xcode versions), so I had assumed it had been fixed. (There have been changes and Apple now recommends that outlets should be strong.)Motionless

© 2022 - 2025 — McMap. All rights reserved.