Friendly King
Friendly King

Reputation: 2496

Swift Package Manager: How to load a UIViewController + xib pair like in a regular Xcode project?

In a regular iOS Xcode project, when you create a Cocoa touch file (MyVC.swift), you can also create an associated xib (MyVC.xib). Call super.init(nibName: nil, bundle: nil) in the view controller, and then the outlets are ready to be used.

In this case, I'm attempting to port over code from my project, into a Swift Package, which will get reused across other projects. When in the Swift Package, I cannot find a way to load the xib for my view controller. After hooking everything up correctly, and calling super.init(nibName:bundle:), the outlets are still always nil at the time of viewDidLoad.

Is this possible in a Swift Package? If so, how; if not, are there any go-to alternatives? This feels like a pretty standard use case.

Fwiw, I'm using swift-tools-version 5.3, and my platform is .iOS(.v13)


A variety of inits that I've tried:

Upvotes: 8

Views: 1924

Answers (1)

Friendly King
Friendly King

Reputation: 2496

After hours of researching to no avail, I found the following Swift forum proposal, which led me to my answer:

super.init(nibName: nil, bundle: .module)

The "module" bundle is what I was looking for: It "Returns the resource bundle associated with the current Swift module."

After using this, my xibs/Storyboards loaded correctly.

Upvotes: 14

Related Questions