green_knight
green_knight

Reputation: 1385

IBOutlets in Swift are nil

(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?

Upvotes: 12

Views: 6055

Answers (3)

7RedBits.com
7RedBits.com

Reputation: 483

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

Upvotes: 1

Yuen Helbig
Yuen Helbig

Reputation: 11

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

Upvotes: 1

rob mayoff
rob mayoff

Reputation: 385970

@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?

Upvotes: 5

Related Questions