Tochukwu
Tochukwu

Reputation: 320

How can i use storyboard in Cocoa Touch Library

I have been able to build a Cocoa Touch library which I intend to use in presenting a ViewController in a storyboard, but it turns out that I can't access the storyboard. I keep getting an NSInvalidArgumentException error, reason: 'Could not find a storyboard named 'MyStoryboard'.

let storyboard: UIStoryboard = UIStoryboard(name: "MyStoryboard", bundle: Bundle.main)
viewController.present(cryptoTransactionViewController, animated: true, completion: nil)

Upvotes: 1

Views: 82

Answers (3)

Tochukwu
Tochukwu

Reputation: 320

When accessing the storyboard ensure that the bundle points to the project your working on.

let bundleId = Bundle(identifier: "com.your.libraryBundle.id)

guard let bundle = Bundle.allFrameworks.first(where: { $0.bundleIdentifier == bundleId}) else { fatalError("Failed to find bundle with identifier (bundleId).") } let storyboard: UIStoryboard = UIStoryboard(name: "MyLibraryStoryboard", bundle: bundle)

Upvotes: 2

Shehata Gamal
Shehata Gamal

Reputation: 100541

Here you access the storyboard from main bundle of the app Bundle.main and for sure doesn't exist , hence the crash

let storyboard: UIStoryboard = UIStoryboard(name: "MyStoryboard", bundle: Bundle.main)

you need to access the library's bundle , so try

let libBundle = Bundle(identifier:"com.library")
let storyboard: UIStoryboard = UIStoryboard(name: "MyStoryboard", bundle:libBundle)

Upvotes: 0

Ashley Mills
Ashley Mills

Reputation: 53231

The storyboard you're trying to access isn't in your main bundle, it's in the framework's bundle.

So, the first thing you need to do is find the correct bundle…

let bundleId = "com.your.bundle.id"
guard let bundle = Bundle.allFrameworks.first(where: { $0.bundleIdentifier == bundleId}) else {
    fatalError("Failed to find bundle with identifier \(bundleId).")
}

let storyboard: UIStoryboard = UIStoryboard(name: "MyStoryboard", bundle: bundle)

Upvotes: 2

Related Questions